Free Ebook cover Creation and maintenance of MongoDB database from basic to advanced

Creation and maintenance of MongoDB database from basic to advanced

5

(1)

88 pages

Updating documents in MongoDB: Updating embedded documents in MongoDB

Capítulo 49

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

Updating documents in MongoDB is an essential task for any database developer. In chapter 10.5 of our e-book, we will focus on how to update documents embedded in MongoDB, a task that may seem complex at first glance, but with the correct understanding it becomes quite simple.

First, let's understand what embedded documents are. In MongoDB, documents can contain subdocuments or inline documents. These embedded documents can be thought of as tables within tables in a relational database. They are useful when we want to store related information in a more organized and efficient way.

To update embedded documents in MongoDB, we use the $set update operator. This operator replaces the value of an existing field with a new value. If the field does not exist, the $set operator will add a new field with the specified value.

Here is an example of how we use the $set operator to update an inline document:

db.collection.update(
   { "_id": 1 },
   { $set:
      {
        "field_name": "new_value"
      }
   }
)

In the above example, we are updating the value of the field 'field_name' to 'new_value' in the document with _id 1 in the specified collection.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

If we want to update a field within an embedded document, the syntax would be slightly different. We need to specify the path to the field in the embedded document. Here is an example:

db.collection.update(
   { "_id": 1 },
   { $set:
      {
        "embedded_document.field": "new_value"
      }
   }
)

In the above example, we are updating the value of the field within the embedded document 'embedded_document' to 'new_value' in the document with _id 1 in the specified collection.

It is important to note that the $set operator does not create the embedded document if it does not exist. If we want MongoDB to create the inline document if it doesn't exist, we need to use the $upsert operator. Here is an example of how we use the $upsert operator:

db.collection.update(
   { "_id": 1 },
   { $set:
      {
        "embedded_document.field": "new_value"
      }
   },
   { upsert: true }
)

In the example above, if the document with _id 1 does not exist in the specified collection, MongoDB will create a new document with the field 'embedded_document.field' set to 'new_value'.

Updating embedded documents in MongoDB is a common and essential task in database development. With the right understanding of update operators and the correct syntax, you can update inline documents easily and efficiently.

I hope this chapter clarified your doubts about updating embedded documents in MongoDB. In the next chapter, we will explore how to delete documents in MongoDB. Stay tuned!

Now answer the exercise about the content:

Which operator is used to update embedded documents in MongoDB and what is its function?

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

You missed! Try again.

The $set operator is used to update embedded documents in MongoDB. Its function is to replace the value of an existing field with a new value or add a new field with the specified value if it does not exist.

Next chapter

Updating documents in MongoDB: Conditional updating documents in MongoDB

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.