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.

CategoryWrite
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

db.<collection>.bulkWrite(<operations>, { ordered: <bool> })

⚙️ Parameters

operations arrayArray of write operation objects, each like {insertOne: {document: ...}}.
orderedOptional. 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏