New in version 3.2.
Performs multiple write operations with controls for order of execution.
bulkWrite()has the following syntax:
Parameter Type Description
An array of
Valid operations are:
See Write Operations for usage of each operation.
document Optional. A document expressing the write concern. Omit to use the default write concern.
Optional. A boolean specifying whether the
mongodinstance should perform an ordered or unordered operation execution. Defaults to
- A boolean
trueif the operation ran with write concern or
falseif write concern was disabled.
- A count for each write operation.
- An array containing an
_idfor each successfully inserted or upserted documents.
- A boolean
bulkWrite() takes an array of write operations and executes each of them. By default operations are executed in order. See Execution of Operations for controlling the order of write operation execution.
Inserts a single document into the collection.
Changed in version 3.6: The
updateMany operations add support for
arrayFilters parameter that determines which elements to modify in an array field. Refer to
db.collection.updateMany() for details.
updateOne updates a single document in the collection that matches the filter. If multiple documents match,
updateOne will update the first matching document only. See
updateMany updates all documents in the collection that match the filter. See
replaceOne replaces a single document in the collection that matches the filter. If multiple documents match,
replaceOne will replace the first matching document only. See
replacement field cannot contain update operators.
deleteOne deletes a single document in the collection that match the filter. If multiple documents match,
deleteOne will delete the first matching document only. See
deleteMany deletes all documents in the collection that match the filter. See
If the document does not specify an _id field, then
mongod adds the
_id field and assign a unique
ObjectId for the document before inserting or upserting it. Most drivers create an ObjectId and insert the
_id field, but the
mongod will create and populate the
_id if the driver or application does not.
If the document contains an
_id field, the
_id value must be unique within the collection to avoid duplicate key error.
Update or replace operations cannot specify an
_id value that differs from the original document.
ordered parameter specifies whether
bulkWrite() will execute operations in order or not. By default, operations are executed in order.
The following code represents a
bulkWrite() with five operations.
In the default
ordered : true state, each operation will be executed in order, from the first operation
insertOne to the last operation
ordered is set to false, operations may be reordered by
mongod to increase performance. Applications should not depend on order of operation execution.
The following code represents an unordered
bulkWrite() with six operations: