MongoDB โ Bulk Write Operations
Batch thousands of insert, update, and delete operations into a single network round-trip using bulkWrite().
1Why Bulk Write Operations Matter
Executing 1,000 separate write calls sends 1,000 individual network request packets to MongoDB. Using bulkWrite() batches all 1,000 operations into a single network request, drastically reducing latency and maximizing throughput.
bulkWrite Code Example
db.products.bulkWrite([
{ insertOne: { document: { title: "Mouse", price: 20 } } },
{ updateOne: { filter: { _id: 101 }, update: { $set: { price: 25 } } } },
{ deleteOne: { filter: { _id: 202 } } }
], { ordered: false });
2Ordered vs Unordered Bulk Execution
- Ordered (Default): MongoDB executes operations serially. If an error occurs on operation #3, processing stops and remaining operations are cancelled.
- Unordered (
{ ordered: false }): MongoDB executes operations in parallel. If an operation fails, MongoDB continues executing all remaining valid operations!