Updating documents lets you change information in your database without deleting and adding new data. This keeps your data accurate and current.
Why updating documents matters in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
db.collection.updateOne(
{ <filter> },
{ $set: { <field1>: <value1>, ... } }
)updateOne changes one document matching the filter.
$set changes only the specified fields without touching others.
Examples
MongoDB
db.users.updateOne({ name: "Alice" }, { $set: { email: "alice@example.com" } })MongoDB
db.orders.updateOne({ orderId: 123 }, { $set: { status: "shipped" } })MongoDB
db.contacts.updateOne({ phone: "123-456" }, { $set: { phone: "987-654" } })Sample Program
This example adds a product, updates its price, then shows the updated product.
MongoDB
db.products.insertOne({ name: "Notebook", price: 10, stock: 100 })
db.products.updateOne({ name: "Notebook" }, { $set: { price: 12 } })
db.products.find({ name: "Notebook" }).pretty()Important Notes
Updating only changes specified fields; other data stays safe.
If no document matches the filter, no update happens.
You can update multiple documents using updateMany instead of updateOne.
Summary
Updating documents keeps your data fresh and correct.
Use $set to change specific fields without losing other data.
Updating is faster and safer than deleting and adding new documents.
Practice
1. Why is it important to update documents in MongoDB instead of deleting and inserting new ones?
easy
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 CQuick 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
Solution
Step 1: Identify correct update operator
The$setoperator updates specific fields without replacing the whole document.Step 2: Check syntax correctness
Only db.collection.updateOne({name: 'John'}, {$set: {age: 30}}) usesupdateOnewith$setcorrectly.Final Answer:
db.collection.updateOne({name: 'John'}, {$set: {age: 30}}) -> Option AQuick Check:
Use $set to update fields = C [OK]
Hint: Use $set inside updateOne to change fields [OK]
Common Mistakes:
- Omitting $set operator
- Using wrong operator like $change or $update
- Passing field directly without $set
3. Given the collection documents:
What will be the result after running:
{"name": "Alice", "score": 50}What will be the result after running:
db.collection.updateOne({name: "Alice"}, {$set: {score: 75}});
db.collection.find({name: "Alice"}).toArray();medium
Solution
Step 1: Understand updateOne with $set
The command changes thescorefield from 50 to 75 for the document wherenameis "Alice".Step 2: Check find query result
The find query returns the updated document withscorenow 75.Final Answer:
[{"name": "Alice", "score": 75}] -> Option BQuick Check:
Update changes score to 75 = D [OK]
Hint: Update changes field value, find shows updated document [OK]
Common Mistakes:
- Expecting old score after update
- Thinking update removes other fields
- Assuming update adds new document
4. What is wrong with this update command?
db.collection.updateOne({name: "Bob"}, {score: 100});medium
Solution
Step 1: Check update command structure
The update document must use an operator like$setto 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 AQuick 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
Solution
Step 1: Use filter to check field existence
The filter{status: {$exists: true}}selects documents wherestatusfield exists.Step 2: Update only matching documents
The$setupdatesstatusto "active" only for those documents, avoiding creating new fields.Final Answer:
db.collection.updateMany({status: {$exists: true}}, {$set: {status: "active"}}) -> Option DQuick 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
- Using $exists:false which matches missing fields
- Filtering with null instead of $exists
