Update Document using Mongo Shell
Introduction
MongoDB CRUD operations are used for various functions such as inserting documents inside a collection, fetching documents from a collection, updating and deleting documents. These operations are heavily used in the back-end. We can use MongoDB CRUD operations from a mongo shell. They are easy to use and have simple syntax. The update operation is one of these CRUD operations that is used commonly at the back-end. In this article, we will discuss the update operation and different ways of updating documents using the mongo shell.
Updating documents in MongoDB
We always need to update data that we earlier stored in the database. In MongoDB, data is stored in documents and MongoDB’s update operation is used to update single or multiple documents.
updateOne
To update a single document, we use updateOne operation. The updateOne operation has three parameters, filter, the update, and options. The filter is the condition to find the document which is to be updated. If no filter is provided, the first document of the collection will be updated. The second parameter is the update that we want in the specific document. We have to use the set operator for this. The third option is optional.
We have a collection, dishes, that contains the name, price, and category of various dishes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | > db.dishes.find().pretty() { "_id" : ObjectId("5d4fe95f1cb588add55bf571"), "dish" : "Cheese Burger", "price" : 20, "category" : "Burgers" } { "_id" : ObjectId("5d4fe97c1cb588add55bf572"), "dish" : "Bacon and Cheese Burger", "price" : 25, "category" : "Burgers" } { "_id" : ObjectId("5d4fe99a1cb588add55bf573"), "dish" : "Pork Pizza", "price" : 30, "category" : "Pizza" } { "_id" : ObjectId("5d4fe9b11cb588add55bf574"), "dish" : "Red and White Pasta", "price" : 15, "category" : "Pasta" } { "_id" : ObjectId("5d4fe9cd1cb588add55bf575"), "dish" : "Mac and Cheese Pasta", "price" : 20, "category" : "Pasta" } > |
Let’s update the price of Cheese Burger using the updateOne operation.
1 2 3 | > db.dishes.updateOne({"dish" : "Cheese Burger"}, {$set: { "price" : 30}}) { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 } > |
Observe the command. For the filter, we specified the dish as Cheese Burger. Now the operation will stop at the document where the dish is Cheese Burger. In the second parameter, we used the set operator to update the price field of the document from 20 to 30. As a result, we got both, matchedCount and modifiedCount as 1. This means that both, our filter and update were successful. Let’s verify it.
1 2 3 4 5 6 7 8 | > db.dishes.find({"dish": "Cheese Burger"}).pretty() { "_id" : ObjectId("5d4fe95f1cb588add55bf571"), "dish" : "Cheese Burger", "price" : 30, "category" : "Burgers" } > |
Observe the document. The price is 30.
updateMany
What if we want to update more than one document? We use updateMany operation for it. The parameters in updateMany are similar to updateOne. The updateOne operation stops when it reaches the first document that matches the filter, but in updateMany operation, it will check for every document that matches the filter. This is how multiple documents are updated.
Let’s update the price of every document where the category is pasta.
1 2 3 | > db.dishes.updateMany({"category" : "Pasta"}, {$set: { "price" : 50}}) { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 } > |
Observe the above command. This operation will find every document where the category is Pasta and then, the price field in each document will be updated to 50. Have a look at the result.
1 | { "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 } |
The matchedCount is 2, that means, two documents were matched. The modifiedCount is also 2, that means, 2 documents were updated. Let’s verify.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | > db.dishes.find({"category": "Pasta"}).pretty() { "_id" : ObjectId("5d4fe9b11cb588add55bf574"), "dish" : "Red and White Pasta", "price" : 50, "category" : "Pasta" } { "_id" : ObjectId("5d4fe9cd1cb588add55bf575"), "dish" : "Mac and Cheese Pasta", "price" : 50, "category" : "Pasta" } > |
The price field of every document is updated to 50 where the category is Pasta.
upsert
The upsert in one of the options that we can use in the third parameter. The upsert option is used when we are not sure that the condition in filter will match with any document. If the upsert option is used, and no document matches the filter condition, a new document will be created with the values specified in the filter and update parameters.
Let’s try updating a document with the filter condition that does not match with any of the documents present in the dishes collection.
1 2 3 | > db.dishes.updateOne({"dish" : "Salted Sandwich"}, {$set: { "price": "15"}}) { "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 } > |
We tried to update the price of a document where dish field has value, Salt Sandwich. Observe the matchedCount and modifiedCount. They both have zero as their values. This means, no document was matched, thus, no document was updated.
If we had used the upsert option with the command, a new document would have been added to the dishes collection. Let’s try it.
1 2 3 4 5 6 7 8 | > db.dishes.updateOne({"dish" : "Salted Sandwich"}, {$set: { "price": "15"}}, {upsert: true}) { "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0, "upsertedId" : ObjectId("5d4ff5d413fa57905434453c") } > |
This time, we added a third parameter with an upsert option and set it to true. In the result, matchedCount and modifiedCount are still 0 because the filter condition didn’t match any document. But there is one other field in the result,i.e, the upsertedId. This proves that upsert was successful. Let’s verify.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | > db.dishes.find().pretty() { "_id" : ObjectId("5d4fe95f1cb588add55bf571"), "dish" : "Cheese Burger", "price" : 30, "category" : "Burgers" } { "_id" : ObjectId("5d4fe97c1cb588add55bf572"), "dish" : "Bacon and Cheese Burger", "price" : 25, "category" : "Burgers" } { "_id" : ObjectId("5d4fe99a1cb588add55bf573"), "dish" : "Pork Pizza", "price" : 30, "category" : "Pizza" } { "_id" : ObjectId("5d4fe9b11cb588add55bf574"), "dish" : "Red and White Pasta", "price" : 50, "category" : "Pasta" } { "_id" : ObjectId("5d4fe9cd1cb588add55bf575"), "dish" : "Mac and Cheese Pasta", "price" : 50, "category" : "Pasta" } { "_id" : ObjectId("5d4ff5d413fa57905434453c"), "dish" : "Salted Sandwich", "price" : "15" } > |
Check the last document. The dish and price field has the same values that we used in the updateOne operation. Observe carefully, the _id and upsertedId(that we got earlier when upsert option was used with update operation) are the same. This is how upsert option works.
Points to remember while using update operation
- Using the updateOne operation without a filter will result in updating the first document of the collection.
- Using the updateMany operation without a filter will result in updating each and every document of the collection.
- The upsert option is optional and is false by default.
Conclusion
The update operation is an important CRUD operation is every database system. We always need to update the data stored in the database. MongoDB provides updateOne and updateMany operations to update the documents. We discussed how to use these operations successfully with different possibilities and options.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started