bulk operation


What is a bulk operation?

Bulk operations are actions that are performed on a large scale. For example, bulk operations tasks include importing or exporting items, changing prices on a mass scale, and assigning products to a warehouse. For each task of a bulk operation, the system creates a message that is published in a message queue.

What is a bulk operation in MongoDB?

Description. Bulk() Bulk operations builder used to construct a list of write operations to perform in bulk for a single collection. To instantiate the builder, use either the db. Collection.

Bulk operations perform a large number of write operations. Instead of making a call for each operation to the database, bulk operations perform multiple operations with one call to the database.

Bulk Write

Pass a slice of WriteModel documents to the BulkWrite() method to perform a bulk operation.

Modify Behavior

The BulkWrite() method optionally takes a BulkWriteOptions type, representing options you can use to modify its behavior. If you don't specify BulkWriteOptions, the driver uses the default values for each option.

Return Values

The BulkWrite() method returns a BulkWriteResult type, which contains information about the bulk operation if it's successful. The BulkWriteResult type includes the following properties:

Operations

A WriteModel represents an insert, replace, update, or delete operation.

Insert

To perform an insert operation, create an InsertOneModel specifying the document you want to insert. Create an InsertOneModel for each document you wish to insert multiple documents.

Replace

To perform a replace operation, create a ReplaceOneModel specifying the document you want to replace and a replacement document. Create a ReplaceOneModel for each document you wish to return to replace multiple documents.

Update

To perform an update operation, create a UpdateOneModel specifying the document you want to update and an updated document. To update multiple documents, use the UpdateManyModel.

Delete

Create a DeleteOneModel specifying the document you want to delete to perform a delete operation. To delete multiple documents, use the DeleteManyModel.

Execution Order

The BulkWrite() method allows you to specify if you want to execute the bulk operations as ordered or unordered in its BulkWriteOptions.

Ordered

The BulkWrite() method executes bulk operations in the order you added them and stops if an error occurs.

To execute bulk write operations in any order and continue if an error occurs, specify false to the SetOrdered() method. The method reports the errors afterward.