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.

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.

Article image Updating documents in MongoDB: Conditional updating documents in MongoDB

Next page of the Free Ebook:

50Updating documents in MongoDB: Conditional updating documents in MongoDB

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou 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