Bulk write operations in MongoDB - Time & Space Complexity
When using bulk write operations in MongoDB, it's important to understand how the time taken grows as you add more operations.
We want to know how the total work changes when we increase the number of write commands in a bulk request.
Analyze the time complexity of the following code snippet.
const bulkOps = [
{ insertOne: { document: { name: "Alice" } } },
{ updateOne: { filter: { name: "Bob" }, update: { $set: { age: 30 } } } },
{ deleteOne: { filter: { name: "Charlie" } } },
// ... more operations
];
collection.bulkWrite(bulkOps);
This code sends a list of different write operations to MongoDB to be executed together in one bulk request.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each write operation in the bulk list is processed one by one.
- How many times: The number of times equals the number of operations in the bulk array.
As you add more operations to the bulk list, the total work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 write actions |
| 100 | About 100 write actions |
| 1000 | About 1000 write actions |
Pattern observation: Doubling the number of operations roughly doubles the total work.
Time Complexity: O(n)
This means the time to complete the bulk write grows linearly with the number of operations you include.
[X] Wrong: "Bulk write operations run in constant time no matter how many writes are included."
[OK] Correct: Each write still needs to be processed, so more operations mean more work and more time.
Understanding how bulk writes scale helps you explain how to handle many database changes efficiently in real projects.
"What if we split the bulk write into multiple smaller bulk writes? How would the time complexity change?"
Practice
bulkWrite() in MongoDB?Solution
Step 1: Understand what
bulkWrite()doesbulkWrite()lets you send many write commands (insert, update, delete) in one call.Step 2: Identify the benefit
Sending many operations at once reduces network overhead and speeds up database changes.Final Answer:
It allows multiple write operations in a single request, improving performance. -> Option AQuick Check:
Bulk write = multiple writes in one call [OK]
- Thinking bulkWrite only reads data
- Believing it backs up data automatically
- Assuming it requires separate calls per operation
Solution
Step 1: Check the bulkWrite syntax
bulkWrite expects an array of operation objects like{ insertOne: { document: {...} } }or{ updateOne: { filter: {...}, update: {...} } }.Step 2: Match options to syntax
db.collection.bulkWrite([{ insertOne: { document: { name: 'Alice' } } }, { updateOne: { filter: { name: 'Bob' }, update: { $set: { age: 30 } } } }]) correctly usesinsertOneandupdateOnewith proper nested objects. Others miss brackets or use wrong keys.Final Answer:
db.collection.bulkWrite([{ insertOne: { document: { name: 'Alice' } } }, { updateOne: { filter: { name: 'Bob' }, update: { $set: { age: 30 } } } }]) -> Option AQuick Check:
Correct bulkWrite syntax = db.collection.bulkWrite([{ insertOne: { document: { name: 'Alice' } } }, { updateOne: { filter: { name: 'Bob' }, update: { $set: { age: 30 } } } }]) [OK]
- Omitting the array brackets []
- Using wrong keys like 'insert' instead of 'insertOne'
- Not nesting filter and update properly
db.users.bulkWrite([
{ insertOne: { document: { _id: 1, name: 'John' } } },
{ updateOne: { filter: { _id: 1 }, update: { $set: { age: 25 } } } },
{ deleteOne: { filter: { name: 'John' } } }
])Solution
Step 1: Analyze each bulkWrite operation in order
First, insert { _id: 1, name: 'John' }. Second, update that document to add age: 25. Third, delete the document where name is 'John'.Step 2: Determine final collection state
After deletion, the document is removed, so the collection is empty.Final Answer:
The collection will be empty after the operations. -> Option CQuick Check:
Insert + update + delete = empty collection [OK]
- Assuming update adds a new document
- Ignoring the delete operation
- Thinking operations run independently
db.products.bulkWrite([
{ insertOne: { document: { _id: 101, name: 'Pen', price: 10 } } },
{ updateOne: { filter: { _id: 101 }, update: { name: 'Pencil' } } }
])Solution
Step 1: Review the updateOne operation
The update document must use an update operator like$setto modify fields.Step 2: Identify the missing operator
Here,{ name: 'Pencil' }is used directly, which replaces the entire document (losing thepricefield). It should be{ $set: { name: 'Pencil' } }.Final Answer:
The update document is missing the $set operator. -> Option DQuick Check:
Update needs $set for field changes [OK]
- Forgetting $set in updateOne
- Thinking insertOne needs $set
- Believing bulkWrite can't mix operations
bulkWrite(). You have these operations:- Update user with _id 1 to set age to 30
- Update user with _id 2 to set age to 25
- Insert a new user with _id 3 and name 'Anna'
Which bulkWrite array correctly performs all these changes in one call?
Solution
Step 1: Check each updateOne operation
Both updates correctly useupdateOnewith$setto change age for _id 1 and 2.Step 2: Verify insertOne operation
[ { updateOne: { filter: { _id: 1 }, update: { $set: { age: 30 } } } }, { updateOne: { filter: { _id: 2 }, update: { $set: { age: 25 } } } }, { insertOne: { document: { _id: 3, name: 'Anna' } } } ] inserts a new document with _id 3 and name 'Anna', which matches the requirement.Step 3: Compare with other options
[ { updateMany: { filter: { _id: { $in: [1, 2] } }, update: { $set: { age: 30 } } } }, { insertOne: { document: { _id: 3, name: 'Anna' } } } ] uses updateMany incorrectly (age 30 for both, but second should be 25). [ { updateOne: { filter: { _id: 1 }, update: { age: 30 } } }, { updateOne: { filter: { _id: 2 }, update: { age: 25 } } }, { insertOne: { document: { _id: 3, name: 'Anna' } } } ] misses $set. [ { updateOne: { filter: { _id: 1 }, update: { $set: { age: 30 } } } }, { updateOne: { filter: { _id: 2 }, update: { $set: { age: 25 } } } }, { insertOne: { document: { name: 'Anna' } } } ] misses _id in insert.Final Answer:
[ { updateOne: { filter: { _id: 1 }, update: { $set: { age: 30 } } } }, { updateOne: { filter: { _id: 2 }, update: { $set: { age: 25 } } } }, { insertOne: { document: { _id: 3, name: 'Anna' } } } ] -> Option BQuick Check:
Use updateOne with $set and insertOne with full document [OK]
- Using update without $set
- Missing _id in insert document
- Using updateMany when individual updates differ
Ready to prove it?
Pass the test and this pattern moves unknown
