Updated March 8, 2023
Definition of MongoDB Bulk Update
Mongodb bulk update method will add the multiple update operations into the bulk update operations list, this method will update the specified field from the existing documents. We have used bulk. find () method to find which documents we have updated from collection and bulk. find.update () method will update all matching documents from the collection. We can also use bulk. find.updateOne () method to update the single document from the collection. We have using a set operator with a bulk update method to set the updated value from the field.
Syntax:
Below is the syntax of the MongoDB bulk update.
Var bulk = db.collection_name.initializeOrderedBulkOp ();
Bulk.find ( { field_name: value } ) .update ( { $set { field_name1: updated_value, field_name1: updated_value } });
bulk.execute ();
Parameter description syntax of MongoDB bulk update
1) Collection name – This is defined as the name of the collection which was we have used to update the bulk documents. We can update multiple documents from the collection by using the bulk update method in MongoDB.
2) InitializeOrderedBulkOp – This method is used with the bulk update method to update multiple documents from the collection. We need to call this method at the time of using the bulk update method in MongoDB.
3) Bulk. find – This method is used to find the documents which were we want to update from collections. After finding the documents we have updated the same in collections.
4) Field name – This is defined as a collections field to update the value from it. We have to update the old value into the field which was we have used in our query.
5) Value – Using this parameter we have replaced the old value with a new value of the collections field. We need to define old and new values at the time of updating documents from the collection field.
6) Bulk. find.update – This method will update the multiple documents which were matched from our query. Using this method we can update multiple documents from the collection.
7) Set operator – We have using this operator with the update method to set a new value of documents. At the time of updating documents in MongoDB, we need to use a set operator.
8) Bulk. execute – This method is executing bulk queries in a single command. We have used this method with bulk. find.update method in MongoDB.
How bulk update works in MongoDB?
- MongoDB bulk update method basically used to update multiple documents from the collection.
- Using the bulk method in MongoDB we can perform bulk operations like update, delete and insert on MongoDB collections.
- We need to use initializeOrderedBulkOp () method to construct the write operations order list that MongoDB will run the bulk operations.
- There are two types of bulk operations builder available in MongoDB to construct the ordered and unordered list of operations.
1) initializeOrderedBulkOp ()
2) initializeUnorderedBulkOp ()
The first method is used to construct an ordered list of write operations from collections in MongoDB.
The second method is used to construct an unordered list of write operations from collections in MongoDB.
We have also using the bulk method to update multiple documents from a collection in MongoDB. The bulk method is used to create the list of write operations to perform the bulk operations on a single collection.
We can use either initializeOrderedBulkOp () or initializeUnorderedBulkOp () method to perform bulk operations in MongoDB.
We need to use bulk. execute () method to run all the operations which was built by bulk methods.
From MongoDB version, 4.2 bulk update methods will accept the pipeline aggregation. Before version 4.2 pipeline aggregation is not available for bulk update methods.
Set stage in bulk update method will provide same behavior which can provide by the set operator.
To use aggregation variables in the bulk update method we need to prefix the variable with the $$ sign.
Without using prefix as $$ we cannot use aggregation variable with bulk update method in MongoDB.
We can also use the upsert method with the bulk update method to insert the documents if not available into the collection.
The bulk update method will update the specific field from the documents which were we need to update from the collection.
We have used bulk. find () method to use the condition that which documents we have updated from the collection.
We have passed the update parameter with the bulk update method in MongoDB.
Example
Below is an example of a bulk update in MongoDB.
1) MongoDB bulk update using initializeUnorderedBulkOp () builder
In the below example, we have to update multiple documents from MongoDB_Update collection by using initializeUnorderedBulkOp () builder.
We can see that we have updated all the documents which contain the name PQR. All the documents were updated from the name PQR to ABC.
Code:
db.MongoDB_Update.find ()
var bulk = db.MongoDB_Update.initializeUnrderedBulkOp ();
bulk.find( { name: "PQR" } ).update( { $set: { name: "ABC"} } );
bulk.execute ();
db.MongoDB_Update.find ()
Figure – Example of MongoDB bulk update using initializeUnorderedBulkOp () builder.
2) MongoDB bulk update using initializeOrderedBulkOp () builder
In below example, we have updating multiple documents form MongoDB_Update collection by using initializeOrderedBulkOp () builder.
We can see that we have updated all the documents which name contains as ABC. All the documents were updated from the name ABC to PQR.
Code:
db.MongoDB_Update.find ()
var bulk = db.MongoDB_Update.initializeOrderedBulkOp ();
bulk.find( { name: "ABC" } ).update( { $set: { name: "PQR"} } );
bulk.execute ();
db.MongoDB_Update.find ()
Figure – Example of MongoDB bulk update using initializeOrderedBulkOp () builder.
3) MongoDB bulk update using multiple field
In the below example, we have to update multiple documents from MongoDB_Update collection by using initializeOrderedBulkOp () builder with multiple fields.
We can see that we have updated all the documents which name contains PQR. All the documents which contain the field name as name are updated from PQR to XYZ, also we can see that the modified date is updated to “12-07-2021” and the points field is updated to 10.
Code:
db.MongoDB_Update.find ()
var bulk = db.MongoDB_Update.initializeUnorderedBulkOp ();
bulk.find( { name: "PQR" } ).update( { $set: { name: "XYZ", modified_date: "12-07-2021", points: 10} } );
bulk.execute ();
db.MongoDB_Update.find ()
Figure – Example of MongoDB bulk update using multiple field.
Conclusion
MongoDB bulk update is very useful in MongoDB to update the multiple documents from a collection that match the selection criteria. We have using initializeOrderedBulkOp () and initializeUnorderedBulkOp () method with the bulk update method. We can also use the aggregation pipeline after version 4.2 with the bulk update method in MongoDB.
Recommended Articles
This is a guide to MongoDB Bulk Update. Here we discuss the definition, How bulk update works in MongoDB? along with examples respectively. You may also have a look at the following articles to learn more –