Why updating documents matters in MongoDB - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
When we update documents in MongoDB, the time it takes can change depending on how many documents we touch and what we do to them.
We want to understand how the cost of updating grows as we update more documents.
Analyze the time complexity of the following code snippet.
// Update all users with age over 30 to set status to 'active'
db.users.updateMany(
{ age: { $gt: 30 } },
{ $set: { status: 'active' } }
)
This code updates many documents that match a condition by changing a field value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning documents to find matches and updating each matched document.
- How many times: Once for each document that matches the condition (age > 30).
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and updates if all match |
| 100 | About 100 checks and updates if all match |
| 1000 | About 1000 checks and updates if all match |
Pattern observation: The work grows roughly in direct proportion to how many documents match and need updating.
Time Complexity: O(n)
This means the time to update grows linearly with the number of documents that match the update condition.
[X] Wrong: "Updating documents is always very fast and takes the same time no matter how many documents are updated."
[OK] Correct: Actually, the more documents that match the update, the more work MongoDB must do, so the time grows with the number of matched documents.
Understanding how updates scale helps you explain how databases handle changes efficiently and why indexing or limiting updates matters in real projects.
"What if we added an index on the age field? How would the time complexity of finding documents to update change?"
Practice
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]
- Thinking delete-insert is faster
- Believing update removes whole document
- Assuming MongoDB can't update documents
age to 30 in a MongoDB document?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]
- Omitting $set operator
- Using wrong operator like $change or $update
- Passing field directly without $set
{"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();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]
- Expecting old score after update
- Thinking update removes other fields
- Assuming update adds new document
db.collection.updateOne({name: "Bob"}, {score: 100});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]
- Omitting $set and replacing document
- Thinking updateOne syntax is wrong
- Believing updateOne can't update numbers
status field to "active" only if it currently exists in the document. Which update command achieves this safely without creating new fields?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]
- Updating all documents regardless of field existence
- Using $exists:false which matches missing fields
- Filtering with null instead of $exists
