MongoDB – All Positional Operator ($[])

MongoDB provides different types of array update operators to update the values of the array fields in the documents and all positional operator($[]) is one of them. This operator indicates that the update operation should modify all the items present in the specified array field.
Syntax:

{ <update operator>: { "<array>.$[]" : <value> } }
  • You can also use this operator for those queries which traverse more than one array and nested arrays.
  • If upsert is set to true, then the query must contain an exact equality match on the array field in order to use $[] operator in the update statement. If upsert operation doesnot include the exact equality match on the array field, then upsert will give an error.
  • You can use $[] operator with update(), findAndModify(), etc., methods to modify all the array items for the document or documents that match the specified query condition.

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 all the items in an array:

In this example, we are updating by incrementing all the items by 10 of points field.



filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({}, {$inc: {"points.$[]": 5}}, {multi: true})

chevron_right


Updating all the documents in the array:

In this example, we are updating by decrementing the value of the tArticles field by -10 for all the items in the articles array.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({}, {$inc: {"articles.$[].tArticles": -10}},
                           {multi: true})

chevron_right


Updating an array using a negation query operator:

In this example, we are incrementing all the items in the points array by 20 for all documents except those those with the value 100 in the points array.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({points: {$ne: 25}},
                      {$inc: {"points.$[]": 20}}, 
                        {multi: true})

chevron_right


Updating the nested array in conjunction with $[< identifier>]:

In this example, we are updating all the values that are less than or equal to 80 in the nested marks.firstsemester array.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.update({}, {$inc: {"marks.$[].firstsemester.$[newmarks]": 3}}, 
                            {arrayFilters: [{newmarks: {$lte: 80}}], multi: true})

chevron_right





My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.