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.
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.
Pass a slice of WriteModel documents to the BulkWrite() method to perform a bulk operation.
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.
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:
A WriteModel represents an insert, replace, update, or delete operation.
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.
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.
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.
Create a DeleteOneModel specifying the document you want to delete to perform a delete operation. To delete multiple documents, use the DeleteManyModel.
The BulkWrite() method allows you to specify if you want to execute the bulk operations as ordered or unordered in its BulkWriteOptions.
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.