bulkWrite() (Bulk Mixed Operations)
Last updated: 2026-10-09
bulkWrite() (Bulk Mixed Operations)
bulkWrite() executes several write operations of mixed types in a single call: insertOne, updateOne, updateMany, replaceOne, deleteOne, and deleteMany. Each operation is an object with an operation type plus its payload, e.g. `db.users.bulkWrite([{ insertOne: { document: { name: 'A' } } }, { updateOne: { filter: { name: 'B' }, update: { $set: { age: 1 } } } }])`. The result groups counters by operation type.
| Category | Write |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
db.<collection>.bulkWrite(<operations>, { ordered: <bool> })
⚙️ Parameters
| operations array | Array of write operation objects, each like {insertOne: {document: ...}}. |
|---|---|
| ordered | Optional. true stops at the first error, false continues; default true. |
Returns:BulkWriteResult, a counter object grouped by operation type.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// bulkWrite: insert, update and delete in one call
db.users.bulkWrite([
{ insertOne: { document: { name: 'Dana', age: 28 } } },
{ updateOne: { filter: { name: 'Alice' }, update: { $set: { age: 23 } } } },
{ deleteOne: { filter: { name: 'Bob' } } }
])
Output:
{
acknowledged: true,
insertedCount: 1,
matchedCount: 1,
modifiedCount: 1,
deletedCount: 1
}
📌 Notes
- Operation types: insertOne/updateOne/updateMany/replaceOne/deleteOne/deleteMany
- With ordered: false, failed operations are skipped and the rest continue
- updateOne and friends accept an embedded upsert option
🏷️ Related Tags
💡 Related Cases
Inserting Documents: A Detailed Explanation of `insertOne` and `insertMany`
Mongoose Advanced Patterns: Performance and Flexibility
Schema Validation: Data Validation at the Database Layer
📚 Related Tutorials
❓ FAQ
QIs bulkWrite faster than calling methods in a loop?
AbulkWrite() sends one request for all operations, far faster than looping single-method calls — ideal for migrations and imports.
QWhich operation types does bulkWrite support?
AIt supports insertOne, updateOne, updateMany, replaceOne, deleteOne, and deleteMany, mixable in one call.
QHow does the ordered option work in bulkWrite?
Aordered: true (default) stops at the first error; false skips failed operations and continues — good for batch jobs where partial success is acceptable.