Docs Home → Develop Applications → MongoDB Manual
db.collection.initializeOrderedBulkOp()![](/docs/v5.3/assets/link.svg)
On this page
Tip
Starting in version 3.2, MongoDB also provides the
db.collection.bulkWrite()
method for performing bulk
write operations.
Definition![](/docs/v5.3/assets/link.svg)
db.collection.initializeOrderedBulkOp()
Important
mongosh Method
This is a
mongosh
method. This is not the documentation forNode.js
or other programming language specific driver methods.In most cases,
mongosh
methods work the same way as the legacymongo
shell methods. However, some legacy methods are unavailable inmongosh
.For the legacy
mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Initializes and returns a new
Bulk()
operations builder for a collection. The builder constructs an ordered list of write operations that MongoDB executes in bulk.Returns: new Bulk()
operations builder object.
Behavior![](/docs/v5.3/assets/link.svg)
Order of Operation![](/docs/v5.3/assets/link.svg)
With an ordered operations list, MongoDB executes the write operations in the list serially.
Execution of Operations![](/docs/v5.3/assets/link.svg)
When executing an ordered
list of operations, MongoDB
groups the operations by the operation type
and
contiguity; i.e. contiguous operations of the same type are grouped
together. For example, if an ordered list has two insert operations
followed by an update operation followed by another insert operation,
MongoDB groups the operations into three separate groups: first group
contains the two insert operations, second group contains the update
operation, and the third group contains the last insert operation. This
behavior is subject to change in future versions.
Bulk()
operations in mongosh
and comparable
methods in the drivers do not have a limit for the number of operations
in a group. To see how the operations are grouped for bulk operation
execution, call Bulk.getOperations()
after the execution.
Tip
See also:
Executing an ordered
list of operations on a
sharded collection will generally be slower than executing an
unordered
list
since with an ordered list, each operation must wait for the previous
operation to finish.
Error Handling![](/docs/v5.3/assets/link.svg)
If an error occurs during the processing of one of the write operations, MongoDB will return without processing any remaining write operations in the list.
Examples![](/docs/v5.3/assets/link.svg)
The following initializes a Bulk()
operations builder on the
users
collection, adds a series of write operations, and executes
the operations:
var bulk = db.users.initializeOrderedBulkOp(); bulk.insert( { user: "abc123", status: "A", points: 0 } ); bulk.insert( { user: "ijk123", status: "A", points: 0 } ); bulk.insert( { user: "mop123", status: "P", points: 0 } ); bulk.find( { status: "D" } ).delete(); bulk.find( { status: "P" } ).update( { $set: { comment: "Pending" } } ); bulk.execute();