📖 Scenario: You are managing a small online bookstore database using MongoDB. You want to update book records efficiently without replacing entire documents. This helps keep your data accurate and your database fast.
🎯 Goal: Build a MongoDB update operation that changes specific fields in book documents using advanced update operators.
📋 What You'll Learn
Create a collection called books with three book documents having fields title, author, and copies_sold.
Add a variable minCopiesSold set to 1000 to use as a threshold.
Use the $inc operator to increase copies_sold by 500 for books with copies_sold greater than or equal to minCopiesSold.
Use the $set operator to add a new field bestseller set to true for those updated books.
💡 Why This Matters
🌍 Real World
Online stores and content platforms often need to update parts of their data quickly without rewriting entire records. Advanced updates let them do this efficiently.
💼 Career
Database developers and administrators use advanced update operators daily to maintain data accuracy and optimize performance in real applications.
Progress0 / 4 steps
1
Create the books collection with initial data
Create a MongoDB collection called books and insert these three documents exactly: { title: 'Learn MongoDB', author: 'Alice', copies_sold: 1200 }, { title: 'Node.js Basics', author: 'Bob', copies_sold: 800 }, and { title: 'Advanced MongoDB', author: 'Charlie', copies_sold: 1500 }.
MongoDB
Hint
Use db.books.insertMany([...]) with the exact documents inside the array.
2
Add the minCopiesSold threshold variable
Create a variable called minCopiesSold and set it to 1000. This will be used to select books with enough sales.
MongoDB
Hint
Use const minCopiesSold = 1000 to create the variable.
3
Use $inc to increase copies_sold for popular books
Write a MongoDB update command that uses $inc to add 500 to copies_sold for all books where copies_sold is greater than or equal to minCopiesSold.
MongoDB
Hint
Use db.books.updateMany with a filter on copies_sold and the $inc operator.
4
Add bestseller field with $set for updated books
Extend the previous update command to also use $set to add a new field bestseller set to true for the same books where copies_sold was increased.
MongoDB
Hint
Combine $inc and $set inside the same update document.
Practice
(1/5)
1. What is the main benefit of using advanced update operators like $set and $inc in MongoDB?
easy
A. They convert documents into arrays.
B. They automatically create new collections.
C. They delete documents faster than normal deletes.
D. They allow updating multiple fields in a single command.
Solution
Step 1: Understand update operators purpose
Operators like $set and $inc let you change specific fields without replacing the whole document.
Step 2: Recognize efficiency in updates
Using these operators, you can update many fields at once, saving time and keeping data accurate.
Final Answer:
They allow updating multiple fields in a single command. -> Option D
Quick Check:
Advanced updates = multiple fields updated at once [OK]
Hint: Advanced updates change many fields in one go [OK]
Common Mistakes:
Thinking updates create new collections
Confusing update with delete operations
Believing updates convert data types automatically
2. Which of the following is the correct syntax to increment a field named score by 5 in MongoDB?
easy
A. { $add: { score: 5 } }
B. { $set: { score: +5 } }
C. { $inc: { score: 5 } }
D. { $push: { score: 5 } }
Solution
Step 1: Identify the operator for increment
The $inc operator is used to increase a numeric field by a specified amount.
Step 2: Check syntax correctness
The correct syntax is { $inc: { score: 5 } }. Other options use wrong operators or syntax.
Final Answer:
{ $inc: { score: 5 } } -> Option C
Quick Check:
Increment uses $inc with field and value [OK]
Hint: Use $inc to add numbers, syntax: { $inc: { field: value } } [OK]
Common Mistakes:
Using $set with +5 instead of $inc
Using $push which is for arrays
Using non-existent $add operator
3. Given the document { _id: 1, tags: ["red", "blue"] }, what will be the result after running db.collection.updateOne({ _id: 1 }, { $push: { tags: "green" } })?
medium
A. { _id: 1, tags: ["red", "blue", "green"] }
B. { _id: 1, tags: ["red", "blue"] }
C. { _id: 1, tags: "green" }
D. Error: $push cannot be used on arrays
Solution
Step 1: Understand $push operator effect
The $push operator adds an element to the end of an array field.
Step 2: Apply $push to the tags array
Adding "green" to the existing array ["red", "blue"] results in ["red", "blue", "green"].
B. Missing update operator like $set before the field.
C. The value 10 must be a string.
D. updateOne cannot update a single document.
Solution
Step 1: Check update command structure
MongoDB update commands require an update operator like $set to specify fields to change.
Step 2: Identify missing operator
The command uses { score: 10 } directly without $set, causing an error.
Final Answer:
Missing update operator like $set before the field. -> Option B
Quick Check:
Update needs $set or similar operator [OK]
Hint: Always use $set or $inc for field updates [OK]
Common Mistakes:
Omitting $set operator
Thinking filter {_id: 2} is wrong
Believing numeric values must be strings
5. You want to update a user's document to increase their points by 10, add a new tag "vip" to their tags array only if it does not exist, and set their status to "active". Which update command achieves all this in one operation?