![]() |
VOOZH | about |
In the area of MongoDB, managing a database with a large collection of documents can be challenging especially when it comes to updating specific objects within arrays of nested objects. This scenario is common in NoSQL databases like MongoDB.
In this article, we’ll explore some methods for updating objects within a document's array in MongoDB. Whether a MongoDB beginner or an experienced user by mastering these techniques will enhance our database management skills and simplify our data manipulation processes.
When working with MongoDB, updating objects within an array requires specific handling to ensure that only the intended objects are modified while leaving the rest of the document unchanged.
MongoDB provides several methods to achieve this. Below is the method that helps us to Update Objects in a Document's Array (Nested Updating) in MongoDB.
The updateOne() method is utilized to modify a single document that meets the specified filter criteria within a collection. If multiple documents match the filter, only the first document encountered is updated.
Syntax:
db.collection.updateOne(
<filter>,
<update>,
{
arrayFilters: [ { <filterCondition> } ],
multi: true
}
)
Explanation:
Suppose we have a gfgdatabase in coursescollection which stores information about various courses. Each document in the collection represents a course and contains details such as the course name, Instructor name, fees, and duration.
After inserting a record into the courses collection, Our courses look like:
Query:
Let's Update the "duration" field of the "HTML" course in a MongoDB collection to "5 weeks" using the positional operator.
db.collection.update(
{ "_id": 1, "courses.name": "HTML" },
{ "$set": { "courses.$.duration": "5 weeks" } }
)
Output:
Explanation: This query updates the "duration" field of the nestedobject within the "courses" array where the "name" field is "HTML" and the "_id" of the document is 1. The positional operator $ is used to update the matching element in the "courses" array.
The updateMany() method is used to update all documents that match the specified filter criteria in a collection.
Syntax:
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>,
collation: <document>,
hint: <document|string> // Available starting in MongoDB 4.2
arrayFilters: [ ... ]
}
)
Explanation:
Query:
Let's Update the "fees" field of all courses in a MongoDB collection to "2000" using the all positional operator.
db.courses.updateMany(
{},
{ "$set": { "courses.$[].fees": "2000" } }
)
Output:
Explanation: In the query db.courses.updateMany({}, { "$set": { "courses.$[].fees": "2000" } }) updates the "fees" field of all nested objects within the "courses" array in every document of the "courses" collection to "2000". The positional operator $[] is used to update all elements of the "courses" array in each document.
Overall, updating objects within a document's array in MongoDB requires careful consideration to ensure that only the intended objects are modified. The `updateOne` method updates a single document that meets the specified filter criteria, whereas the `updateMany` method modifies all documents that fulfill the same criteria.
By using these methods, along with the arrayFilters option, developers can efficiently update nested objects in MongoDB arrays. Understanding these techniques not only improves your MongoDB skills but also optimizes your overall data management strategies in distributed applications.