OrderedBulkOperation()

Constructor

Create a new OrderedBulkOperation instance (INTERNAL TYPE, do not instantiate directly)

class OrderedBulkOperation()
Arguments:
  • collection (object) – collection instance.
  • [options] (object) – additional options for the collection.
Returns:

object a ordered bulk operation instance.

Options
  • w, {Number/String, > -1 || ‘majority’ || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = ‘majority’ or tag acknowledges the write
  • wtimeout, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
  • fsync, (Boolean, default:false) write waits for fsync before returning, from MongoDB 2.6 on, fsync cannot be combined with journal
  • j, (Boolean, default:false) write waits for journal sync before returning

update

Add a single update document to the bulk operation

update(doc)
Arguments:
  • doc (object) – update operations
Returns:

orderedbulkoperation

Examples

Example of a simple ordered insert/update/upsert/remove ordered collection

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');

var db = new Db('test', new Server('localhost', 27017));
  // Establish connection to db
  db.open(function(err, db) {
    // Get the collection
    var col = db.collection('batch_write_ordered_ops_0');
    // Initialize the Ordered Batch
    var batch = col.initializeOrderedBulkOp();

    // Add some operations to be executed in order
    batch.insert({a:1});
    batch.find({a:1}).updateOne({$set: {b:1}});
    batch.find({a:2}).upsert().updateOne({$set: {b:2}});
    batch.insert({a:3});
    batch.find({a:3}).remove({a:3});

    // Execute the operations
    batch.execute(function(err, result) {
      // Check state of result
      assert.equal(2, result.nInserted);
      assert.equal(1, result.nUpserted);
      assert.equal(1, result.nMatched);
      assert.ok(1 == result.nModified || result.nModified == null);
      assert.equal(1, result.nRemoved);

      var upserts = result.getUpsertedIds();
      assert.equal(1, upserts.length);
      assert.equal(2, upserts[0].index);
      assert.ok(upserts[0]._id != null);

      var upsert = result.getUpsertedIdAt(0);
      assert.equal(2, upsert.index);
      assert.ok(upsert._id != null);

      // Finish up test
      db.close();
    });
  });

updateOne

Add a single update one document to the bulk operation

updateOne(doc)
Arguments:
  • doc (object) – update operations
Returns:

orderedbulkoperation

replaceOne

Add a replace one operation to the bulk operation

replaceOne(doc)
Arguments:
  • doc (object) – the new document to replace the existing one with
Returns:

orderedbulkoperation

upsert

Upsert modifier for update bulk operation

upsert()
Returns:orderedbulkoperation

removeOne

Add a remove one operation to the bulk operation

removeOne(doc)
Arguments:
  • doc (object) – selector for the removal of documents
Returns:

orderedbulkoperation

remove

Add a remove operation to the bulk operation

remove(doc)
Arguments:
  • doc (object) – selector for the single document to remove
Returns:

orderedbulkoperation

insert

Add a single insert document to the bulk operation

insert(doc)
Arguments:
  • doc (object) – the document to insert
Returns:

orderedbulkoperation

find

Initiate a find operation for an update/updateOne/remove/removeOne/replaceOne

find(doc)
Arguments:
  • doc (object) –
Returns:

orderedbulkoperation

execute

Execute the ordered bulk operation

Options
  • w, {Number/String, > -1 || ‘majority’ || tag name} the write concern for the operation where &lt; 1 is no acknowlegement of write and w >= 1, w = ‘majority’ or tag acknowledges the write
  • wtimeout, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
  • fsync, (Boolean, default:false) write waits for fsync before returning, from MongoDB 2.6 on, fsync cannot be combined with journal
  • j, (Boolean, default:false) write waits for journal sync before returning
execute([options], callback)
Arguments:
  • [options] (object) – additional options during update.
  • callback (function) – this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from from the ordered bulk operation.
Returns:

null

Contents

Manual

MongoDB Wiki