Open In App

How to Updating a Single Subelement in an Array Referenced by Index in MongoDB

In MongoDB, arrays are a powerful feature for storing multiple values within a single document. However, updating a specific element within an array, especially by its index can be challenging without a clear understanding of MongoDB’s update operations.

In this article, we’ll explore how to update a single subelement in an array referenced by its index in MongoDB by covering concepts and examples for better understanding.



How to Updating a Single Subelement in an Array Referenced by Index in MongoDB

Let’s set up an Environment:

To understand How to Find Items Without a Certain Field in MongoDB we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called products which contains the information shown below:



[
{
_id: ObjectId('60f9d7ac345b7c9df348a871'),
name: 'Product D',
tags: [ 'electronics', 'tablet', 'gadgets' ]
},
{
_id: ObjectId('60f9d7ac345b7c9df348a872'),
name: 'Product E',
tags: [ 'electronics', 'smartwatch', 'wearables' ]
},
{
_id: ObjectId('60f9d7ac345b7c9df348a873'),
name: 'Product F',
tags: [ 'electronics', 'camera', 'photography' ]
}
]

Examples of Updating a Single Subelement in the Array

To update a specific subelement within the “tags” array, we’ll use the $set operator along with array indexing. Let’s consider an example where we want to update the second element (“smartphone”) in the “tags” array to “mobile”.

Example 1

Let’s update the “completed” field of the second step in the “steps” array to mark it as completed:

db.products.updateOne(
{ "_id": ObjectId("60f9d7ac345b7c9df348a873") }, // Filter criteria
{ $set: { "tags.1": "mobile" } } // Update operation
);

Output:

[
{
_id: ObjectId('60f9d7ac345b7c9df348a871'),
name: 'Product D',
tags: [ 'electronics', 'tablet', 'gadgets' ]
},
{
_id: ObjectId('60f9d7ac345b7c9df348a872'),
name: 'Product E',
tags: [ 'electronics', 'smartwatch', 'wearables' ]
},
{
_id: ObjectId('60f9d7ac345b7c9df348a873'),
name: 'Product F',
tags: [ 'electronics', 'mobile', 'photography' ]
}
]

Explanation: This MongoDB updateOne command updates a single document in the products collection. It uses the filter criteria { "_id": ObjectId("60f9d7ac345b7c9df348a873") } to find the document to update. The update operation { $set: { "tags.1": "mobile" } } sets the value of the second element ("tags.1") in the tags array to "mobile".

EXAMPLE 2:

db.products.updateOne(
{ "_id": ObjectId("60f9d7ac345b7c9df348a871") }, // Filter criteria
{ $set: { "tags.1": "mobile" } } // Update operation
);

Output:

[
{
_id: ObjectId('60f9d7ac345b7c9df348a871'),
name: 'Product D',
tags: [ 'electronics', 'DESKTOP', 'gadgets' ]
},
{
_id: ObjectId('60f9d7ac345b7c9df348a872'),
name: 'Product E',
tags: [ 'electronics', 'smartwatch', 'wearables' ]
},
{
_id: ObjectId('60f9d7ac345b7c9df348a873'),
name: 'Product F',
tags: [ 'electronics', 'mobile', 'photography' ]
}
]

Explanation: This MongoDB updateOne command updates a single document in the products collection. It uses the filter criteria { "_id": ObjectId("60f9d7ac345b7c9df348a871") } to find the document to update. The update operation { $set: { "tags.1": "DESKTOP" } } sets the value of the second element ("tags.1") in the tags array to "DESKTOP".

Conclusion

Overall, Updating a single subelement in an array by its index in MongoDB involves using the $set operator combined with array indexing. This approach allows us to modify specific elements within arrays, providing flexibility and control over document updates. In this article, we explored how to update a subelement within an array in a MongoDB collection using a practical example. By understanding and applying these concepts, you can effectively manage and manipulate array data within MongoDB documents.

Article Tags :