Introduction
Bulk write operations let you do many changes to a database at once. This saves time and makes your work faster.
db.collection.bulkWrite([
{ insertOne: { document: { /* data */ } } },
{ updateOne: { filter: { /* condition */ }, update: { /* changes */ } } },
{ deleteOne: { filter: { /* condition */ } } },
{ replaceOne: { filter: { /* condition */ }, replacement: { /* new data */ } } }
])db.users.bulkWrite([
{ insertOne: { document: { name: "Alice", age: 25 } } },
{ updateOne: { filter: { name: "Bob" }, update: { $set: { age: 30 } } } },
{ deleteOne: { filter: { name: "Charlie" } } }
])db.products.bulkWrite([
{ replaceOne: { filter: { sku: "123" }, replacement: { sku: "123", name: "New Product", price: 20 } } }
])db.inventory.bulkWrite([
{ insertOne: { document: { item: "pen", qty: 50 } } },
{ updateOne: { filter: { item: "notebook" }, update: { $set: { qty: 100 } } } },
{ deleteOne: { filter: { item: "eraser" } } }
])bulkWrite() in MongoDB?bulkWrite() doesbulkWrite() lets you send many write commands (insert, update, delete) in one call.{ insertOne: { document: {...} } } or { updateOne: { filter: {...}, update: {...} } }.insertOne and updateOne with proper nested objects. Others miss brackets or use wrong keys.db.users.bulkWrite([
{ insertOne: { document: { _id: 1, name: 'John' } } },
{ updateOne: { filter: { _id: 1 }, update: { $set: { age: 25 } } } },
{ deleteOne: { filter: { name: 'John' } } }
])db.products.bulkWrite([
{ insertOne: { document: { _id: 101, name: 'Pen', price: 10 } } },
{ updateOne: { filter: { _id: 101 }, update: { name: 'Pencil' } } }
])$set to modify fields.{ name: 'Pencil' } is used directly, which replaces the entire document (losing the price field). It should be { $set: { name: 'Pencil' } }.bulkWrite(). You have these operations:updateOne with $set to change age for _id 1 and 2.Pass the test and this pattern moves unknown