Class: BulkOperationBase

BulkOperationBase

Parent class to OrderedBulkOperation and UnorderedBulkOperation

NOTE: Internal Type, do not instantiate directly

new BulkOperationBase()

Create a new OrderedBulkOperation or UnorderedBulkOperation instance

Methods

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:
  • Throws error if the bulk object has already been executed

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

    Type
    MongoError
Returns:
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

Throws:

if a selector is not specified

Type
MongoError
Returns:
helper object with which the write operation can be defined.
Example

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

Throws:
Returns:
reference to self
Example

const bulkOp = collection.initializeOrderedBulkOp();
// Adds three inserts to the bulkOp.
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.

Returns:
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.