Lesson 3: Updating MongoDB Documents by Using findAndModify() / Learn

Press O for more options
2:36

Video Transcript (English)

Welcome! In this video, you’ll learn how to update and return the same document in MongoDB.

We’ll cover how and when to use the `findAndModify()` method. We’ll also explain how `findAndModify()` is different from updateOne().

`findAndModify()` is used to return the document that has just been updated.

For example, imagine we’re creating an app that tracks the number of users who download a podcast.

One way to update and return the number of downloads is to use the `updateOne()` and `findOne()` methods. First, we would use `updateOne()` to increment the downloads field. Then, we would use `findOne()` to return the document by using the `_id`.

The problem with this approach is that it makes two round trips to the server, where `findAndModify()` is only one. Additionally, if we make our update via `updateOne`, another user could modify the document before our `findOne()` and we will get a different version of the document.

To prevent this, we can use `findAndModify()`. This is a powerful method that guarantees the correct version of the document will be returned without another thread modifying the document before we are able to view it.

Let’s take a look at an example of the document we want to modify. Notice the `downloads` field is currently set to 6012.

Here, we use `findAndModify` to increment the downloads field by 1.

This method accepts a document as an argument with several embedded document fields. The fields we’ll focus on are

the `query` field, which accepts a query document,

the `update` field, which accepts an update document,

and the `new` field, which accepts a boolean value.

By default, the returned document does not include the modifications made as part of the update. Set the `new` option to `true` so that the modified document is returned.

In this example, the query field is set to the `_id` for the podcast document. The update field is set to a document that includes the modification we want to make to the downloads field. And, the `new` option is set to `true`, which will return the updated document shown here.

Now let’s recap what you learned in this video.

We use the `findAndModify()` method to update and return the document that matches our query. We set the `new` option to `true` to make sure new version of the updated document is returned.

The best approach to updating documents depends on the situation, but using `findAndModify()` guarantees that the exact document you just updated will be returned.