Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why Updating Documents Matters in MongoDB
📖 Scenario: You are managing a small online bookstore database using MongoDB. You have a collection of books, and sometimes you need to update the information about a book, like its price or stock quantity, to keep the data accurate and useful for customers.
🎯 Goal: Learn how to update documents in a MongoDB collection to keep your data current and correct.
📋 What You'll Learn
Create a collection called books with three book documents
Add a variable to specify the book to update
Write an update query to change the price of the specified book
Add a final query to confirm the update was successful
💡 Why This Matters
🌍 Real World
Updating documents keeps your database accurate, which is important for online stores, inventory systems, and any app that changes data over time.
💼 Career
Database administrators and backend developers often update documents to reflect real-world changes, such as price changes, stock updates, or user information edits.
Progress0 / 4 steps
1
Create the books collection with initial documents
Create a books collection with these exact three documents: { title: "The Great Gatsby", author: "F. Scott Fitzgerald", price: 10, stock: 5 }, { title: "1984", author: "George Orwell", price: 15, stock: 8 }, and { title: "To Kill a Mockingbird", author: "Harper Lee", price: 12, stock: 7 }.
MongoDB
Hint
Use db.books.insertMany() with an array of objects to add multiple documents at once.
2
Specify the book to update
Create a variable called bookToUpdate and set it to { title: "1984" } to select the book you want to update.
MongoDB
Hint
Use const bookToUpdate = { title: "1984" } to create the variable.
3
Update the price of the selected book
Write an update query using db.books.updateOne() to change the price of the book selected by bookToUpdate to 18.
MongoDB
Hint
Use db.books.updateOne(bookToUpdate, { $set: { price: 18 } }) to update the price.
4
Confirm the update by finding the updated document
Write a query using db.books.findOne() with bookToUpdate to retrieve the updated book document.
MongoDB
Hint
Use const updatedBook = db.books.findOne(bookToUpdate) to get the updated document.
Practice
(1/5)
1. Why is it important to update documents in MongoDB instead of deleting and inserting new ones?
easy
A. Deleting and inserting is faster and safer.
B. Updating deletes the entire document automatically.
C. Updating keeps data consistent and avoids losing other fields.
D. MongoDB does not support updating documents.
Solution
Step 1: Understand document update purpose
Updating modifies only specific fields, keeping other data intact.
Step 2: Compare update vs delete-insert
Deleting and inserting risks losing data and is slower than updating.
Final Answer:
Updating keeps data consistent and avoids losing other fields. -> Option C
Quick Check:
Update preserves data = B [OK]
Hint: Update changes fields without losing data [OK]
Common Mistakes:
Thinking delete-insert is faster
Believing update removes whole document
Assuming MongoDB can't update documents
2. Which of the following is the correct syntax to update the field age to 30 in a MongoDB document?
easy
A. db.collection.updateOne({name: 'John'}, {$set: {age: 30}})
B. db.collection.updateOne({name: 'John'}, {age: 30})
C. db.collection.update({name: 'John'}, {$change: {age: 30}})
D. db.collection.updateOne({name: 'John'}, {$update: {age: 30}})
Solution
Step 1: Identify correct update operator
The $set operator updates specific fields without replacing the whole document.
Step 2: Check syntax correctness
Only db.collection.updateOne({name: 'John'}, {$set: {age: 30}}) uses updateOne with $set correctly.
Final Answer:
db.collection.updateOne({name: 'John'}, {$set: {age: 30}}) -> Option A
Quick Check:
Use $set to update fields = C [OK]
Hint: Use $set inside updateOne to change fields [OK]
A. Missing $set operator, so it replaces the whole document.
B. The filter query is incorrect syntax.
C. updateOne cannot update numeric fields.
D. The collection name is invalid.
Solution
Step 1: Check update command structure
The update document must use an operator like $set to update fields without replacing the whole document.
Step 2: Understand effect of missing $set
Without $set, the document is replaced entirely with {score: 100}, losing other fields.
Final Answer:
Missing $set operator, so it replaces the whole document. -> Option A
Quick Check:
Always use $set to update fields [OK]
Hint: Always include $set to update fields safely [OK]
Common Mistakes:
Omitting $set and replacing document
Thinking updateOne syntax is wrong
Believing updateOne can't update numbers
5. You want to update the status field to "active" only if it currently exists in the document. Which update command achieves this safely without creating new fields?
hard
A. db.collection.updateMany({status: {$exists: false}}, {$set: {status: "active"}})
B. db.collection.updateMany({}, {$set: {status: "active"}})
C. db.collection.updateMany({status: null}, {$set: {status: "active"}})
D. db.collection.updateMany({status: {$exists: true}}, {$set: {status: "active"}})
Solution
Step 1: Use filter to check field existence
The filter {status: {$exists: true}} selects documents where status field exists.
Step 2: Update only matching documents
The $set updates status to "active" only for those documents, avoiding creating new fields.
Final Answer:
db.collection.updateMany({status: {$exists: true}}, {$set: {status: "active"}}) -> Option D
Quick Check:
Filter with $exists true to update safely = A [OK]
Hint: Filter with $exists:true to update only existing fields [OK]
Common Mistakes:
Updating all documents regardless of field existence