Definition
Mongo.bulkWrite(operations, options)
bulkWrite()
performs multiple write operations across multiple databases and collections in a single call. This method replaces db.collection.bulkWrite()
, which performs multiple write operations to a specific collection in a single call.
Note
You can only use Mongo.bulkWrite()
with MongoDB 8.0 or later.
Syntax
You can call bulkWrite()
on the current Mongo()
instance by using the following syntax:
db.getMongo().bulkWrite(
[
{
namespace: "<db1.collection1>",
name: "insertOne",
document: { ... }
},
{
namespace: "<db2.collection2>",
name: "replaceOne",
filter: { ... }
}
],
{
ordered: boolean,
verboseResults: boolean,
bypassDocumentValidation: boolean,
let: Document
}
)
You can also call it on a different Mongo
instance, like in the following example:
const otherMongo = Mongo("<other connection string>");
otherMongo.bulkWrite([{ namespace: "<db.collection>", ... }]);
bulkWrite()
accepts two parameters:
Parameter | Type | Description |
---|---|---|
| Array of documents | Defines an array of write operations. Each document in the array represents a write operation that you want to execute. |
| Document | Defines options for the operation. |
A document in operations
can represent one of six operations:
- insert one
- replace one
- update one
- update many
- delete one
- delete many
The following sections describe the syntax you must use for documents that represent each operation.
Insert One
{
namespace: '<db.collection>',
name: 'insertOne',
document: Document
}
Field | Type | Description |
---|---|---|
| String | The database and collection that you want to insert the document into. |
| String | The operation. Set to |
| Document | The document that you want to insert. |
Note
If you do not specify an _id
field in the document to insert, mongosh
automatically generates an _id
.
Update One or Many
{
namespace: '<db>.<collection>',
name: 'updateOne' | 'updateMany',
filter: Document,
update: Document | Document[],
arrayFilters?: Document[],
hint?: Document | string,
collation?: Document,
upsert?: boolean
}
Field | Type | Description |
---|---|---|
| String | The database and collection that you want to update documents in. |
| String | The operation. Set to |
| Document | The filter that matches one or more documents you want to update. During an update one operation, |
| Document | The update to perform. |
| Array of documents | (Optional) If you update an array-valued field, |
| Document or string | (Optional) The index to use for the operation. |
| Document | (Optional) The collation to use when sorting results. |
| Boolean | Specifies whether MongoDB creates a new document if no document matches the filter. Defaults to |
Replace One
{
namespace: '<db>.<collection>',
name: 'replaceOne',
filter: Document,
replacement: Document,
hint?: Document | string,
collation?: Document
}
Field | Type | Description |
---|---|---|
| String | The database and collection that you want to replace documents in. |
| String | The operation. Set to |
| Document | The filter that matches the document you want to update. During a replace one operation, |
| Document | The replacement document. |
| Document or string | (Optional) The index to use for the operation. |
| Document | (Optional) The collation to use when sorting results. |
Delete One or Many
{
namespace: '<db>.<collection>',
name: 'deleteOne' | 'deleteMany',
filter: Document,
hint?: Document | string,
collation?: Document
}
Field | Type | Description |
---|---|---|
| String | The database and collection that you want to delete documents in. |
| String | The operation. Set to |
| Document | The filter that matches the document you want to delete. During a delete one operation, |
| Document or string | (Optional) The index to use for the operation. |
| Document | (Optional) The collation to use when sorting results. |
Options
You can use the following options with bulkWrite()
. You can pass a document to bulkWrite()
that contains the options that you want to use. This document is optional.
{
ordered?: boolean,
verboseResults?: boolean,
bypassDocumentValidation?: boolean,
let?: Document
}
Field | Type | Description |
---|---|---|
| Boolean | (Optional) Indicates that MongoDB performs the bulk write in order of the documents that you provide. Defaults to |
| Boolean | (Optional) Specifies if |
| Boolean | (Optional) Specifies if the write operation bypasses document validation rules. Defaults to |
| Document | (Optional) Document of parameter names and values that you can access with aggregation variables. |
Output
bulkWrite()
returns an object with the following fields:
{
acknowledged: boolean,
insertedCount: int,
matchedCount: int,
modifiedCount: int,
deletedCount: int,
upsertedCount: int,
insertResults?: map(int, document),
updateResults?: map(int, document),
deleteResults?: map(int, document)
}
Field | Type | Description |
---|---|---|
| boolean |
|
| integer | Number of documents inserted. |
| integer | Number of documents matched by filter. |
| integer | Number of documents modified. |
| integer | Number of documents deleted. |
| integer | Number of documents upserted. |
| Map of integers to documents | Optional. Represents the results of each successful insert operation. Each operation is represented by an integer key, which contains a document with information corresponding to the operation. Document includes the following field:
|
| Map of integers to documents | Optional. Represents the results of each successful update operation. Each operation is represented by an integer key, which contains a document with information corresponding to the operation. Document includes the following fields:
|
| Map of integers to documents | Optional. Represents the results of each successful delete operation. Each operation is represented by an integer key, which contains a document with information corresponding to the operation. Document includes the following field:
|
Examples
This example uses Mongo.bulkWrite()
to perform the following operations in order:
- inserts a document into the
db.authors
collection - inserts a document into the
db.books
collection - updates the previous document
db.getMongo().bulkWrite(
[
{
namespace: 'db.authors',
name: 'insertOne',
document: { name: 'Stephen King' }
},
{
namespace: 'db.books',
name: 'insertOne',
document: { name: 'It' }
},
{
namespace: 'db.books',
name: 'updateOne',
filter: { name: 'it' },
update: { $set: { year: 1986 } }
}
],
{
ordered: true,
bypassDocumentValidation: true
}
)
mongosh
performs the bulk write in order and returns the following document:
{
acknowledged: true,
insertedCount: 2,
matchedCount: 1,
modifiedCount: 1,
deletedCount: 0,
upsertedCount: 0,
insertResults: { '1': { insertedId: ObjectId('67ed8ce8efd926c84cab7945') },
'2': { insertedId: ObjectId('67ed8ce8efd926c84cab7946') } }
updateResults: { '1': { matchedCount: 1, modifiedCount: 1, didUpsert: false } }
}