db.collection.initializeUnorderedBulkOp()
Tip
Starting in version 3.2, MongoDB also provides thedb.collection.bulkWrite()
method for performing bulkwrite operations.
Definition
mongo
Shell Method
This page documents the mongo
shell method, and doesnot refer to the MongoDB Node.js driver (or any other driver)method. For corresponding MongoDB driver API, refer to your specificMongoDB driver documentation instead.
New in version 2.6.
Initializes and returns a new Bulk()
operations builderfor a collection. The builder constructs an unordered list ofwrite operations that MongoDB executes in bulk.
Behavior
Order of Operation
With an unordered operations list, MongoDB can execute in parallelthe write operations in the list and in any order. If the order ofoperations matter, usedb.collection.initializeOrderedBulkOp()
instead.
Execution of Operations
When executing an unordered
list of operations,MongoDB groups the operations. With an unordered bulk operation, theoperations in the list may be reordered to increase performance. Assuch, applications should not depend on the ordering when performingunordered
bulkoperations.
Each group of operations can have at most 1000 operations
. If a group exceeds this limit
, MongoDB will divide the group intosmaller groups of 1000 or less. For example, if the bulk operations listconsists of 2000 insert operations, MongoDB creates 2 groups, each with1000 operations.
The sizes and grouping mechanics are internal performance details andare subject to change in future versions.
To see how the operations are grouped for a bulk operation execution,call Bulk.getOperations()
after the execution.
Error Handling
If an error occurs during the processing of one of the writeoperations, MongoDB will continue to process remaining write operationsin the list.
Example
The following initializes a Bulk()
operations builder andadds a series of insert operations to add multiple documents:
- var bulk = db.users.initializeUnorderedBulkOp();
- 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.execute();
See also