Article image Updating documents in MongoDB: Using the findAndModify command in MongoDB

10.7. Updating documents in MongoDB: Using the findAndModify command in MongoDB

Page 51 | Listen in audio

In any database application, one of the most common operations is updating records. In MongoDB, you can do this using the findAndModify command. This command is very useful because it allows you to find and update a document in a single operation.

To better understand how the findAndModify command works, let's consider an example. Suppose we have a collection called 'students' with the following document:

{
    "_id" : ObjectId("5f6d1a87e87a5e3a3cfebae8"),
    "name" : "John Doe",
    "age": 20,
    "courses" : ["Math", "Science"]
}

If we want to update John Doe's age to 21, we can use the findAndModify command as follows:

db.students.findAndModify({
    query: { name: "John Doe" },
    update: { $set: { age: 21 } }
});

The above command searches for a document in the 'students' collection where the 'name' field equals 'John Doe'. It then updates the age of that document to 21.

An important thing to note about the findAndModify command is that it returns the document before updating by default. If you want it to return the document after updating, you can pass the 'new' option as true. For example:

db.students.findAndModify({
    query: { name: "John Doe" },
    update: { $set: { age: 21 } },
    new:true
});

Another useful option you can pass to the findAndModify command is 'upsert'. If you set 'upsert' to true, MongoDB will create a new document if no document matches the query. For example:

db.students.findAndModify({
    query: { name: "Jane Doe" },
    update: { $set: { age: 22 } },
    new:true,
    upsert: true
});

In the above command, if there is no document where the 'name' field is equal to 'Jane Doe', MongoDB will create a new document with 'name' as 'Jane Doe' and 'age' as 22.

p>

The findAndModify command also allows you to update multiple fields at once. For example, if we want to update John Doe's age to 21 and add 'English' to the list of courses he is taking, we can do this like this:

db.students.findAndModify({
    query: { name: "John Doe" },
    update: { $set: { age: 21 }, $push: { courses: "English" } },
    new:true
});

Additionally, the findAndModify command allows you to use MongoDB update operators such as $set, $inc, $push, $pull, and so on. This makes the findAndModify command a very powerful tool for updating documents in MongoDB.

In summary, the findAndModify command is an efficient way to find and update documents in MongoDB. It offers many options that allow you to customize the behavior of the update operation, making it an essential tool for any MongoDB developer.

Understanding how to use commands like findAndModify is critical to working effectively with MongoDB. By mastering these techniques, you will be able to create and maintain efficient, high-performance MongoDB databases.

Now answer the exercise about the content:

What is the function of the findAndModify command in MongoDB?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Updating documents in MongoDB: Best practices for updating documents in MongoDB

Next page of the Free Ebook:

52Updating documents in MongoDB: Best practices for updating documents in MongoDB

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text