Bulk.getOperations()
New in version 2.6.
Returns an array of write operations executed throughBulk.execute()
. The returned write operations are ingroups as determined by MongoDB for execution. For information onhow MongoDB groups the list of bulk write operations, seeBulk.execute() Behavior.
Only use Bulk.getOperations()
after aBulk.execute()
. Calling Bulk.getOperations()
before you call Bulk.execute()
will result in anincomplete list.
Example
The following initializes a Bulk()
operations builder on theitems
collection, adds a series of write operations, executes theoperations, and then calls getOperations()
on thebulk
builder object:
- var bulk = db.items.initializeUnorderedBulkOp();
- for (var i = 1; i <= 1500; i++) {
- bulk.insert( { x: i } );
- }
- bulk.execute();
- bulk.getOperations();
The getOperations()
method returns an array with theoperations executed. The output shows that MongoDB divided theoperations into 2 groups, one with 1000 operations and one with 500.For information on how MongoDB groups the list of bulk writeoperations, see Bulk.execute() Behavior
Although the method returns all 1500 operations in the returned array,this page omits some of the results for brevity.
- [
- {
- "originalZeroIndex" : 0,
- "batchType" : 1,
- "operations" : [
- { "_id" : ObjectId("53a8959f1990ca24d01c6165"), "x" : 1 },
- ... // Content omitted for brevity
- { "_id" : ObjectId("53a8959f1990ca24d01c654c"), "x" : 1000 }
- ]
- },
- {
- "originalZeroIndex" : 1000,
- "batchType" : 1,
- "operations" : [
- { "_id" : ObjectId("53a8959f1990ca24d01c654d"), "x" : 1001 },
- ... // Content omitted for brevity
- { "_id" : ObjectId("53a8959f1990ca24d01c6740"), "x" : 1500 }
- ]
- }
- ]
Returned Fields
The array contains documents with the following fields:
originalZeroIndex
- Specifies the order in which the operation was added to the bulkoperations builder, based on a zero index; e.g. first operationadded to the bulk operations builder will have
originalZeroIndex
value of0
.
batchType
Operation1Insert2Update3Remove
See also
Bulk()
and Bulk.execute()
.