![]() |
VOOZH | about |
The all positional $[] operator in MongoDB updates every element in an array field for documents that match the query, enabling efficient in-place updates across arrays, including arrays of embedded documents.
{ <update operator>: { "<array>.$[]" : <value> } }In the following examples, we are working with:
Database: GeeksforGeeks
Collection: contributor
Document: Two documents that contain the details of the contributor in the form of field-value pairs.
Updating by incrementing all the items by 5 of points field.
db.contributor.updateMany(
{},
{ $inc: { "points.$[]": 5 } }
)
Output:
Updating by decrementing the value of the tArticles field by 10 for all the items in the articles array.
db.contributor.updateMany(
{},
{ $inc: { "articles.$[].tArticles": -10 } }
)
Output:
Incrementing all the items in the points array by 20 for all documents except those with the value 100 in the points array.
db.contributor.updateMany(
{ points: { $nin: [100] } },
{ $inc: { "points.$[]": 20 } }
)
Output:
Updating all the values that are less than or equal to 80 in the nested marks.firstsemester array.
db.contributor.updateMany(
{},
{ $inc: { "marks.$[].firstsemester.$[newmarks]": 3 } },
{ arrayFilters: [{ newmarks: { $lte: 80 } }] }
)
Output:
The $[] positional operator updates all elements in a specified array field for documents that match the query, including nested arrays.