MongoDB Bulk Operation Methods

The MongoDB bulk methods are used to perform bulk operation like bulk write and bulk remove.

#1. db.collection.initializeOrderBulkOp()

The initializeOrderBulkOp and gives a new Bulk() operations builder for the collection. It constructs an ordered list of write operations that MongoDB runs in bulk.

he following initializes a Bulk() operations builder on the users collection, adds a series of write operations, and executes the operations:

#2. db.collection.initializeUnorderedBulkOp()

The method boot up and gives a new Bulk() operations builder for the collection. It constructs an unordered list of write operations that MongoDB runs in bulk.

For Example:

The following initializes a Bulk() operations builder and adds a series of insert operations to add multiple documents:

#3. Bulk() method

Bulk method can be used to create a list of write operations to perform in bulk for one collection. To obejectified the builder, use either the

The Bulk() builder has the following methods:

Bulk.insert()It can be used to add an insert operation inside the list of operations.
Bulk.find()It specifies the query condition for a remove or an update operation.
Bulk.find.removeOne()It adds a remove operation to a list of operations for a single document.
Bulk.find.remove()It adds remove operation to a list of operations for multiple document.
Bulk.find.replaceOne()It adds a document replacement operation to a numbers of operations.
Bulk.find.updateOne()It adds a single document update operation to an array of operations.
Bulk.find.update()It adds a multi update operation to an array of operations.
Bulk.find.upsert()It can be used to specify upsert that is true for an update operation.
Bulk.execute()It executes an array of operations in bulk.
Bulk.getOperations()It gives an array of write operations executed in the Bulk() operations object.
Bulk.tojson()It returns a JSON document that have the number of operations and batches in the Bulk() operations object.
Bulk.toString()It gives the Bulk.tojson() results as a string.

#4. Bulk.execute() method

It runs the list of operations that is built by the Bulk() methods builder.

For example:

Executing Bulk method

The following initializes a Bulk() operations builder on the items collection, adds a series of insert operations, and executes the operations:

#5. Bulk.find(<query>) method

It can be used to specify the query condition for an update or a remove operation. Specifies a query condition using Query Selectors to select documents for an update or a remove operation. To specify all documents, use an empty document {}.

Bulk.find() can be used with the following operations of write command:

For example:

The examples given below initializes a Bulk() operations builder for the items collection and adds a remove operation. It also update the operation to the list of operations. Remove operation and update operation with the help Bulk.find() method, specifies a condition for their respective actions:

#6. Bulk.find.remove() method

It adds a remove operation to a bulk operations list. The Bulk.find() method can be used to specify the condition, which determines the documents to be remove. The Bulk.find.remove() removes all matching documents from the collection.

The example given below initializes a Bulk() operations builder for the items collection and adds remove operation. The remove operation can be used to remove all documents in the collection where the status equals "D":

#7. Bulk.find.replaceOne(<document>) method

It can be used to add a single document replacement operation to a bulk operations list. Use this method to specify the condition that determines the document that has to be replaced. The method limits the replacement to a single document.

A replacement document that completely replaces the existing document. Contains only field and value pairs.

For example

The example given below initializes a Bulk() operations builder for the items collection, and adds various replaceOne operations to the list of operations.

#8. Bulk.find.update(<update>) method

It can be used to add a multi update operation to a bulk operation list. The method updates specific fields for the existing files.

The Bulk.find() method can be used to specify the condition that determines the documents that has to be updated. The method updates all matching documents.

For Example:

The following example initializes a Bulk() operations builder for the items collection, and adds various multi update operations to the list of operations.

#9. Bulk.insert(<document>) method

It can be used to add an insert operation to a bulk operations list.

For example:

The example initializes a Bulk() operations builder for the items collection and adds a series of insert operations to add multiple documents:

#10. Bulk.toString()

It can be used to return the output as a string a JSON document that contains the number of operations and batches inside the Bulk() object.

For example

The example below initializes a Bulk() operations builder on the items collection, adds a series of write operations, and calls Bulk.toString() on the bulk builder object.






Latest Courses