Bulk writes
Send many writes of any kind in a single request.
Send many writes in a single request with bulkWrite. It saves network round trips when you load or sync many documents.
Requirements
- A connection with a user that has
readWriteon the database.
Send a batch
JavaScript
db.orders.bulkWrite(
[
{ insertOne: { document: { customer: "Eva", total: 20 } } },
{ updateOne: { filter: { customer: "Ana Ruiz" }, update: { $set: { vip: true } } } },
{ deleteMany: { filter: { status: "cancelled" } } },
],
{ ordered: false },
);With ordered: true, the batch stops at the first error; with false, it goes on and returns every error.
Check
The response carries how many documents were inserted and how many were updated or deleted. It also carries the errors of each operation.
Common problems
- The batch fails with
InvalidLength(16) because it has no operations or more than a batch accepts.