Class: BulkOperationBase


Parent class to OrderedBulkOperation and UnorderedBulkOperation

NOTE: Internal Type, do not instantiate directly

new BulkOperationBase()

Create a new OrderedBulkOperation or UnorderedBulkOperation instance

Name Type Description
length number

Get the number of operations in the bulk.


execute(_writeConcern, options, callback){Promise|void}

Execute the bulk operation

Name Type Description
_writeConcern WriteConcern optional

Optional write concern. Can also be specified through options.

options object optional

Optional settings.

Name Type Default Description
w number | string optional

The write concern.

wtimeout number optional

The write concern timeout.

j boolean false optional

Specify a journal write concern.

fsync boolean false optional

Specify a file sync write concern.

callback BulkOperationBase~resultCallback optional

A callback that will be invoked when bulkWrite finishes/errors

  • Throws error if the bulk object has already been executed

  • Throws error if the bulk object does not have any operations

Promise if no callback passed

Builds a find operation for an update/updateOne/delete/deleteOne/replaceOne.
Returns a builder object used to complete the definition of the operation.

Name Type Description
selector object

The selector for the bulk operation. See q documentation


if a selector is not specified

helper object with which the write operation can be defined.

const bulkOp = collection.initializeOrderedBulkOp();

// Add an updateOne to the bulkOp
bulkOp.find({ a: 1 }).updateOne({ $set: { b: 2 } });

// Add an updateMany to the bulkOp
bulkOp.find({ c: 3 }).update({ $set: { d: 4 } });

// Add an upsert
bulkOp.find({ e: 5 }).upsert().updateOne({ $set: { f: 6 } });

// Add a deletion
bulkOp.find({ g: 7 }).deleteOne();

// Add a multi deletion
bulkOp.find({ h: 8 }).delete();

// Add a replaceOne
bulkOp.find({ i: 9 }).replaceOne({ j: 10 });

// Update using a pipeline (requires Mongodb 4.2 or higher)
bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([
{ $set: { total: { $sum: [ '$y', '$z' ] } } }

// All of the ops will now be executed
await bulkOp.execute();

Add a single insert document to the bulk operation

Name Type Description
document object

the document to insert

reference to self

const bulkOp = collection.initializeOrderedBulkOp();
// Adds three inserts to the bulkOp.
.insert({ a: 1 })
.insert({ b: 2 })
.insert({ c: 3 });
await bulkOp.execute();

Specifies a raw operation to perform in the bulk write.

Name Type Description
op object

The raw operation to perform.

options.hint object optional

An optional hint for query optimization. See the update command reference for more information.

reference to self

Type Definitions

resultCallback(error, result)

The callback format for results

Name Type Description
error MongoError

An error instance representing the error during the execution.

result BulkWriteResult

The bulk write result.