Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
There are two methods in MongoDB, i.e., updateOne() and updateMany() that can be used to update the existing document or documents in the collection. Upsert is the combination of update and insert methods (update + insert = upsert). So we will first understand how the documents can be updated because upsert will be used with the updateOne and updateMany methods in MongoDB while doing the implementation.
In the article “Upsert in MongoDB”, we will discuss updating the documents in MongoDB using the updateOne() and updateMany() methods, what upsert is in MongoDB with its needs and syntax, and the implementation of the upsert option with the updateOne() updateMany(), and findAndModify() methods in MongoDB.
Updating Documents in MongoDB
In this section of the article “Upsert in MongoDB”, we will discuss how the documents can be updated in the MongoDB collections. MongoDB's updateOne() method can update a single document found first in the collection. The updateMany() method in MongoDB can be used to update multiple documents found in the collection.
Syntax of updateOne and updateMany method
Here is the syntax of the updateOne method in MongoDB that can be used for updating a single document in the collection:
The first insertMany() method inserts multiple documents in the ‘students’ collection in the above commands. Then updateOne() method is called to select the document with the ‘name’ of ‘Ninja C’ and update the value of ‘name’ to ‘Coding Ninja’. Lastly, the find() method is called to print all documents of the ‘students’ collection with the updated ‘name’ attribute of the document found first.
Implementation of updateMany method in MongoDB
In this example, we will use the updateMany method in MongoDB to update multiple documents.
The first insertMany() method inserts multiple documents in the ‘students’ collection in the above commands. Then updateMany() method is called to select the document with the ‘name’ of ‘Ninja C’ and update the value of ‘name’ to ‘Coding Ninja’. Lastly, the find() method is called to print all documents of the ‘students’ collection with the updated ‘name’ attribute of every found document.
upsert Option in MongoDB
An upsert is an option used to insert a new document in the collection of the searched document with the updateOne() or updateMany() method not found. It is a boolean value, true or false, where if the value is true, then the new document will be inserted in the collection. The default value of this option is false, where no new document will not be inserted in the collection.
Why do we need an upsert Option?
Let's understand why we need an upsert option in MongoDB. The updateOne() method updates the first document found in the collection, and the updateMany() method updates all the documents found in the collection, but what if the searched document is not found in the collection? Then, the updateOne() or updateMany() method will not make any change. So, there can be some cases where the updating is forced, so we want to insert the new document with the updated data, which is what the upsert option does.
Here is an example where no searched document in the collection is found, so updateOne() and updateMany() will work the same way.
In the above commands, the first insertMany() method inserts multiple documents in the ‘students’ collection. Then updateMany() method is called to select the document with the ‘name’ of ‘Ninja D’ and update the value of ‘name’ to ‘Coding Ninja’. Lastly, the find() method is called to print all documents of the ‘students’ collection, but there is no document with the ‘name’ of ‘Ninja D’. So, no changes are made to the collection.
Syntax of upsert Option
Here is the syntax of the upsert option in MongoDB to insert a new document in case the searched document is not found in the collection:
upsert: <boolean>
If the upsert option is not specified in the updateOne() or updateMany() method, the default value will be false.
Implementation of upsert Option
In this section of the article “Upsert in MongoDB”, we will implement the upsert option in the updateOne() and updateMany() methods in MongoDB.
upsert with updateOne() method
In this example, we will use the upsert option with the updateOne() method in MongoDB, where the value of upsert will be set to true.
In the above commands, the first insertMany() method inserts multiple documents in the ‘students’ collection. Then updateOne() method is called to select the document with the ‘name’ of ‘Ninja C’ and update the value of ‘name’ to ‘Coding Ninja’. Lastly, the find() method is called to print all documents of the ‘students’ collection, but no document with the ‘name’ of ‘Ninja D’ is found. So, the ‘name’ of ‘Coding Ninja’ is inserted in the collection.
upsert with updateMany() method
In this example, we will use the upsert option with the updateMany() method in MongoDB, where the value of upsert will be set to true.
In the above commands, the first insertMany() method inserts multiple documents in the ‘students’ collection. Then updateMany() method is called to select the document with the ‘name’ of ‘Ninja C’ and update the value of ‘name’ to ‘Coding Ninja’. Lastly, the find() method is called to print all documents of the ‘students’ collection, but no document with the ‘name’ of ‘Ninja D’ is found. So, the ‘name’ of ‘Coding Ninja’ is inserted in the collection.
Upsert with FindAndModify() Method
In this example, we will use the upsert option with the findAndModify() method in MongoDB, where the value of upsert will be set to true.
In the above commands, the first insertMany() method inserts multiple documents in the ‘students’ collection. Then findAndModify() method is called to select the document with the ‘name’ of ‘Ninja E’ and update the value of ‘age’ to ‘30’. Lastly, the find() method is called to print all documents of the ‘students’ collection, but no document with the ‘name’ of ‘Ninja E’ is found. So, the ‘name’ of ‘Ninja E’ and the age of ‘30’ is inserted in the collection.
Upsert with Replacement Document
In this example, we will use the upsert option with the udpateOne() method with a replacement document in MongoDB, where the value of upsert will be set to true.
In the above commands, the first insertMany() method inserts multiple documents in the ‘students’ collection. Then updateOne() method with the replacement document is called to select the document with the ‘name’ of ‘Ninja C’ and update the value of ‘name’ to ‘Coding Ninja’ and ‘age’ to ‘30’. Lastly, the find() method is called to print all documents of the ‘students’ collection.
Upsert with Aggregation Pipeline
In this example, we will use the upsert option with the udpateOne() method in MongoDB, where the value of upsert will be set to true, so the aggregation pipeline will be used automatically.
In the above commands, the first insertMany() method inserts multiple documents in the ‘students’ collection. Then updateOne() method with the replacement document is called to select the document with the ‘name’ of ‘Ninja D’ and update the value of ‘name’ to ‘Coding Ninja’ and ‘age’ to ‘26’. Lastly, the find() method is called to print all documents of the ‘students’ collection.
Upsert with Dotted_id Query
In this example, we will use the upsert option with the udpateOne() method with the Dotted_id query in MongoDB, where the value of upsert will be set to true.
In the above commands, the first insertMany() method inserts multiple documents in the ‘students’ collection. Then updateOne() method with the dotted_id query is called to select the document with the ‘_id.name’ of ‘Ninja D’ and ‘_id.age’ of ‘30’ and insert the document of the value of ‘name’ to ‘Ninja E’ and ‘age’ to ‘26’. Lastly, the find() method is called to print all documents of the ‘students’ collection.
Frequently Asked Questions
What is selection_criteria in MongoDB?
The selection_criteria is the key value pair that is passed to the updateOne() or updateMany() method in MongoDB, by which the method will find the document that matches the given criteria.
What are the benefits of upsert in MongoDB?
Using the upsert option in MongoDB can be used in reducing the need for a separate update and insert operations, to simplify the code, reduce the number of database queries, and provide flexibility in handling different scenarios.
What is the insertMany() method in MongoDB?
The db.<collection>.insertMany() method in MongoDB inserts multiple documents into a particular collection in the database, and insertMany() method in MongoDB cannot insert a single document in the collection.
What is the insertOne() method in MongoDB?
The db.<collection>.insertOne() method in MongoDB inserts a single document into a particular collection in the database, and the insertOne() method in MongoDB cannot insert multiple documents in the collection.
Conclusion
The upsert option in MongoDB is used to insert a new document in the collection of the no document matched with the selection criteria in the updateOne() or updateMany() methods. It is nothing but the combination of insert and update methods in MongoDB.
In the article “Upsert in MongoDB”, we will discuss updating the documents in MongoDB using updateOne() and updateMany() methods, what is upsert in MongoDB with its need, syntax, and the implementation of the upsert option with the updateOne() and updateMany() method in MongoDB. Here are more articles that are recommended to read: