How to Update a Document using the Mongo Shell

Introduction

Updating a record is a basic CRUD method of any database technology and is extremely important to familiarize yourself with it. If your application needs to update the user’s address, phone, or credit card information these would all by typical examples of an update operation. An update is not creating a record but modifying an existing record. In MongoDB we call records documents and will refer to them that way from this point forward. In this step-by-step tutorial we’ll show you how to do a basic update operation using the MongoDB shell.

Prerequisites

  • You should have MongoDB installed and running.
  • It’s recommended that you create a database and collection to experiment with as you follow along.
  • Some command line experience is recommended.

Goal

We always like to start out with an explicit goal to make sure we hit our mark by the end of the tutorial. We currently have an existing demoDatabase and a demoPeopleCollection in that database which contains multiple records. Our goal is to update a single document in the MongoDB shell and also to update multiple documents using a single MongoDB shell command.

Here is the initial demoCollection for comparing before and after the update:

>db.demoPeopleCollection.find()
{
        "_id" : ObjectId("5c7ebf6ad2ea12b0cdc798b5"),
        "First Name" : "Alfred",
        "Last Name" : "Amus",
        "Gender" : "Male",
        "Age" : 39
}
{
        "_id" : ObjectId("5c7ebf6ad2ea12b0cdc798b6"),
        "First Name" : "Bob",
        "Last Name" : "Caster",
        "Gender" : "Male",
        "Age" : 65
}
{
        "_id" : ObjectId("5c7ebf6ad2ea12b0cdc798b7"),
        "First Name" : "Tessa",
        "Last Name" : "Sanders",
        "Gender" : "Female",
        "Age" : 39
}
{
        "_id" : ObjectId("5c7ebf6ad2ea12b0cdc798b8"),
        "First Name" : "Donna",
        "Last Name" : "Dee",
        "Gender" : "Female",
        "Age" : 21
}
{
        "_id" : ObjectId("5c7ebf6ad2ea12b0cdc798b9"),
        "First Name" : "Caitlyn",
        "Last Name" : "Anderson",
        "Gender" : "Female",
        "Age" : 21
}

Update a single document with .update()

To update a single document we’ll use the .update() method with the following basic syntax

db.<collectionName>.update(<selectionCriteria>, <updateData>)

It will only update a single document by default unless you use a special parameter. We show a simple example of updating Donna Dee‘s age to 22.

To only update Donna Dee we use the document to look for records that have Donna as a First Name and Dee as the Last Name. In production it’s more likely you’ll use an explicit _id, just in case there are two Donna Dee’s, but we’ll use this example which better illustrates the selectionCriteria.

> db.demoPeopleCollection.updateOne(
   { "First Name": "Caitlyn", "Last Name": "Anderson" },
   {
     $set: { Age: 22 }
   }
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

We got an acknowledgemnt = true which let’s us know that our update was a success but seeing is believing so we’d like to verify by actually seeing the updated age. To do this we can use a .find() command to query her record and verify the age is now in fact 22.

> db.demoPeopleCollection.find({ "First Name": "Caitlyn", "Last Name": "Anderson" })
{
        "_id" : ObjectId("5c7ede42d2ea12b0cdc79995"),
        "First Name" : "Caitlyn",
        "Last Name" : "Anderson",
        "Gender" : "Female",
        "Age" : 22,
}

Note: We use quotes for field names like “First Name” which have a space in it.

Update a multiple document with .updateMany()

Now if you want to multiple documents at once you should use the .updateMany() method whose basic definition is below:

db.<collectionName>.updateMany(<selectionCriteria>, <updateData>, <options>)

To show how it works, we’ll update “Female” to just “F” for all documents with “Female” as Gender.

> db.demoPeopleCollection.updateMany(
   { Gender: "Female" },
   { $set: { Gender: "F" } }
)
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }

Again we get an acknowledgement = true with a modifiedCount = 3 which means we have successfully updated 3 documents. Let’s double check by verifying with .find() command.

>db.demoPeopleCollection.find({Gender: "F"})
{
        "_id" : ObjectId("5c7ede42d2ea12b0cdc79995"),
        "First Name" : "Caitlyn",
        "Last Name" : "Anderson",
        "Gender" : "F",
        "Age" : 22,
}
{
        "_id" : ObjectId("5c7ede42d2ea12b0cdc79996"),
        "First Name" : "Donna",
        "Last Name" : "Dee",
        "Gender" : "F",
        "Age" : 21
}
{
        "_id" : ObjectId("5c7ede42d2ea12b0cdc79998"),
        "First Name" : "Tessa",
        "Last Name" : "Sanders",
        "Gender" : "F",
        "Age" : 39
}

As you can see the three documents with Gender: "Female" were updated to Gender: "F".

Learn More

To learn more about the updating single and multiple documents we suggest you look up the MongoDB documentation directly or you can always reach out to an expert at Object Rocket to discuss your specific application.

Conclusion

We showed you the basic CRUD operation of update and how to do it in the MongoDB shell. We showed you how to both update a single document as well as multiple documents. If you’d like to learn more about the options available in these functions, the MongoDB documentations is fairly straightforward and can answer many of your questions. We hope this tutorial was able to answer your question or provide you the snippet of code that you were looking for all along. Thank you for your time and if you have any feedback or questions do not hesitate to reach out to us.

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.