Bulk.getOperations()

  • 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:

  1. var bulk = db.items.initializeUnorderedBulkOp();
  2.  
  3. for (var i = 1; i <= 1500; i++) {
  4. bulk.insert( { x: i } );
  5. }
  6.  
  7. bulk.execute();
  8. 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.

  1. [
  2. {
  3. "originalZeroIndex" : 0,
  4. "batchType" : 1,
  5. "operations" : [
  6. { "_id" : ObjectId("53a8959f1990ca24d01c6165"), "x" : 1 },
  7.  
  8. ... // Content omitted for brevity
  9.  
  10. { "_id" : ObjectId("53a8959f1990ca24d01c654c"), "x" : 1000 }
  11. ]
  12. },
  13. {
  14. "originalZeroIndex" : 1000,
  15. "batchType" : 1,
  16. "operations" : [
  17. { "_id" : ObjectId("53a8959f1990ca24d01c654d"), "x" : 1001 },
  18.  
  19. ... // Content omitted for brevity
  20.  
  21. { "_id" : ObjectId("53a8959f1990ca24d01c6740"), "x" : 1500 }
  22. ]
  23. }
  24. ]

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 haveoriginalZeroIndex value of 0.
  • batchType
  • Specifies the write operations type.

batchTypeOperation1Insert2Update3Remove

  • operations
  • Array of documents that contain the details of the operation.

See also

Bulk() and Bulk.execute().