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.

> 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.

> 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.

> 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.

> 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.

{ "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.

> 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.

> 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.

> 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.

> 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

  1. Using the updateOne operation without a filter will result in updating the first document of the collection.
  2. Using the updateMany operation without a filter will result in updating each and every document of the collection.
  3. 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 for 30 Days

It's easy to get started. Imagine the time you'll save by not worrying about database management. Let's do this!

PILOT FREE FOR 30 DAYS

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.