EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB Bulk Update

MongoDB Bulk Update

Updated March 8, 2023

MongoDB Bulk Update

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 ()

MongoDB bulk update 1

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 ()

MongoDB bulk update 2

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 ()

MongoDB bulk update 3

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 –

  1. MongoDB findOne()
  2. MongoDB sort()
  3. MongoDB Database
  4. MongoDB Import
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
37+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
204+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MONGODB Course Bundle - 6 Courses in 1
 20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more