• Post author:
  • Post category:MongoDB
  • Post last modified:May 9, 2024
  • Reading time:8 mins read
You are currently viewing Update Objects in the Array in MongoDB

How to update objects in the document array in MongoDB? We can use the $ positional operator in combination with the updateOne() or updateMany() method to update objects in an array in MongoDB. The $ positional operator allows us to update the first array element that matches a particular condition. This article will focus on how to update objects in a document array in MongoDB and explains how to implement it.

Advertisements

Let’s create a collection student with some documents containing array objects and will use this collection to demonstrate the update.


# Create Collection
db.student.insertMany([{
  _id:1,
  students: [
    {
      name: "Alex",
      age: 25,
      email: "[email protected]",
      scholarship: 8000,
      marks: [45, 67, 89]
    },
    {
      name: "Candice",
      age: 30,
      email: "[email protected]",
      scholarship: 4000,
      marks: [40, 77, 59]
    }
  ]
}
])

1. Update a Single Field Object in the Document Array

Use the updateOne() function with the $set operator and use the . notation to update a single field in an object within a document array in MongoDB. The . notation is used to access the specific field within the object. The $set operator is used to replace the field value with any value that is specified by the user.


# Update objects in the array in MongoDB
db.student.updateOne(
  { _id: 1, "students.name": "Alex" },
  { $set: { "students.$.email": "[email protected]" } }
);

The above query updates a single document in the student collection with the _id of 1 and an object in the students array that has a name field equal to Alex. Then, we called the $set operator which uses the $ positional operator to set the email field of that object to [email protected]. Now, we can see the output which updates the specified field object in the array of the matched document.

Update Objects array in MongoDB

The updated value is shown in the document array below.

Update Objects in MongoDB

2. Update Multiple Field Objects in the Document Array

Similarly, let’s update the multiple object fields in the array, again, the $set operator is utilized along with the . notation to specify the multiple fields.


# Update multiple fields in object within document array.
db.student.updateOne(
  {"students.name": "Candice" },
  { $set: {
    "students.$.email": "[email protected]",
    "students.$.age": 24
  }
  }
);

The above query updates the document in the student collection. It has an object in the students array with a name field equal to Candice. It uses the $ positional operator in the $set operator to set the two fields, the email of that object to [email protected] and then sets the age field of that object to 24. Hence, the update operation matched and modified the objects in the array document in MongoDB as shown in the output.

The modified document array now looks like this.

Update Objects in MongoDB

3. Update to add a field object in the document array

Moreover, we can also use this method to add a new field to an object within a document array. This approach also uses the $set operator along with the . notation to access the specific object, and then simply add the new field and its value.


# Add a new field to an object within a document array
db.student.updateOne(
  { "students.name": "Alex" },
  { $set: { "students.$.course": "MongoDB" } }
);

Here, we have an element in the students array with a name field equal to Alex. The $set operator is specified which is called the $ positional operator adds a new field course with the value MongoDB to the students array. Thus, the output indicates that the field object in the document array is added with the success.  

Now, the document is added with the new field object in the array as shown below.

Update Objects in MongoDB

4. Using the condition

In addition to all the above examples, we can also set the conditions within the updateMany() method to update objects in an array in MongoDB. We have updateMany() method in this example that is utilized to update every document that meets the specified condition.


# Setting the conditions within the updateMany() method
db.student.updateMany(
  { "students.scholarship": { $lte: 8000 } },
  { $mul: { "students.$[].scholarship": 2 } }
);

Here, it finds all documents in the collection where any subdocument of the students array has a scholarship field value that is less than or equal to 8000. Then multiplies the value of the scholarship field of all subdocuments by 2 using the $mul update operator. The output here shows that one document is matched and modified which satisfies the given criteria.

Update Objects in MongoDB

Here, we can see with the find query that subdocuments are updated in the document.

5. Conclusion

In this article, you have learned how to update a field in the array in MongoDB, updating multiple fields within an array is an effective feature of MongoDB that allows for flexible and scalable data management. By using the examples explained in this article we can quickly and easily update the data as needed.

More details about this topic can be found here.

This Post Has One Comment

  1. Rama

    thank you. very useful

Comments are closed.