Computer scienceFundamentalsSQL and DatabasesDBMSMongoDBOperations and operatorsCRUD operations

Update Operations

4 minutes read

Imagine that you have some collection where you have just inserted the data. However, after checking this collection, you've noticed that you had inserted the wrong data in one of the records. What should you do, delete the whole database and create everything from scratch? Of course, not! In this case, you just need to update the value of the wrong inserted field and that's it.

In this topic, you will learn how to update the data in MongoDB Database. Let's begin!

Update a single record

First of all, let's talk about updating just one document in our collection. We will take a collection named movies this time. The collection has a few fields: _id, title, year. Now let's insert some data about the "The Godfather" movie:

{
  _id: 1,
  title: "The Godfather",
  year: 1982
}

Great! But can you spot a mistake? This movie actually premiered in 1972. Let's fix it!

To update a single document, MongoDB has the updateOne() command with the following syntax:

db.<collection>.updateOne(
  <update criteria>,
  {
    $set: {
      <field_to_update>: <updated_data>,
      <field_to_update1>: <updated_data1>,
      ...
    }
  }
)

It may seem a little complicated at first, so let's take a look at an example:

db.movies.updateOne(
  {_id: 1},
  {
    $set: {
      year: 1972
    }
  }
)

We took the _id field as an update criteria to find exactly the movie that we need. As you may know, the _id field should be unique. Then we used the $set operator to update the value of the year field.

So, after all our manipulations, we should have the following result:

{
  _id: 1,
  title: "The Godfather",
  year: 1972
}

The year field is correct now and nobody will get confused about that anymore.

Update many records

In the previous examples, even if we have many records fitting our criteria, the updateOne() method will update just the first document in the collection. Now imagine a situation when you need to update multiple records by some criteria. In this case, the updateMany() method is what we need.

updateMany() has the same syntax as updateOne(), but it will update all the records that meet the established criteria.

For the next example, let's take a staff collection with the _id, name, position, and salary fields. Our manager said to raise developer salaries to $3500. Let's do it:

db.staff.updateMany(
  {position: "developer"},
  {
    $set: {
      salary: "$3500"
    }
  }
)

Now all the employees with the developer position have new salaries. Congratulations are probably in order.

Also, if you want to update all the documents without setting a filter, just specify the empty brackets ({ }) as a criterion. In the case of updateOne() this will update only the first document in the collection.

Replace the document

In this paragraph, we will talk about replacing the documents in the collection. There could be situations in which you need to replace one record with another. You might wonder why can't you just update all the fields using the updateOne() method. But that is not a good practice.

To replace one document with another, MongoDB has the replaceOne() function with the following syntax:

db.<collection>.replaceOne(
   <criteria>,
   <new document>
)

Remember the staff collection? Unfortunately, one of our designers has left. But we found a new ambitious fellow! His name is Mark, and we will pay him $2000. Now we need to replace an old employee with the new one in our database. The previous designer's _id was equal to 783, so our newbie will have the same _id. Let's register our new teammate!

db.staff.replaceOne(
  {_id: 783},
  {
    _id: 783,
    name: "Mark",
    position: "Designer",
    salary: "$2000"
  }
)

Welcome aboard, Mark! And good job to you as now you know how to replace documents in MongoDB.

Conclusion

We've covered a lot of new information in this topic. You have learned three new commands that will help you with updating information. Now it is time to repeat some key facts and move ahead to the practical part!

  • Use updateOne() to update one document in the collection.
  • Use updateMany() to update multiple documents.
  • Use replaceOne() to replace documents.
17 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo