MongoDB – Positional Operator ($)

MongoDB provides different types of array update operators to update the values of the array fields in the documents and positional operator( $ ) is one of them. This operator recognizes an element in an array to update without explicitly specifying the position of that item in the array.

Syntax:

{ "<array>.$" : value }
  • You cannot use the $ operator with upsert operations. If you use $ operator with upsert operations, then the insert will use the $ as a field name in the inserted document.
  • This operator cannot use for those queries which traverse more than one array because the replacement for the $ placeholder is a single value.
  • When you use the $ operator with $unset operator, then $ operator does not remove the matching item from the array, but rather sets it to null.
  • When a query matches the array with the help of negation operators like $not, $ne, or $nin, then you cannot use the $ operator to update the values of that array. And if the negated part of the query is present inside of an $elemMatch expression, then you can use the positional $ operator to update this field.
  • When you use this operator with an update operation like db.collection.update() and db.collection.findAndModify(), then the $ operator works as a placeholder for the first item that matches the query document and the array field is necessary to appear as a part of the query document.

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 values in the array using $ operator:

In this example, we are updating the first item whose value is “Python” to “Ruby” in the language field with the help of $ operator, because we don’t know the position of the item in the array.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.updateOne({name: "Rohit", language: "Python"}, 
                         {$set: {"language.$": "Ruby"}})

chevron_right


Here, the $ operator is working as a placeholder for the first match of the update query document.

Note: The array field must be the part of the query.

Updating documents in the array using $ operator:

In this example, we are updating an array that contains embedded documents with the help of $ operator and to access embedded document fields we use dot notation. Or in other words, we are updating the value of tArticle field from 60 to 100.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.updateOne({name: "Rohit", "articles.language": "C#"}, 
                         {$set: {"articles.$.tArticles": 100}})

chevron_right


Updating embedded documents using multiple field matches:

In this example, we are updating the value of the tArticles field in the first embedded document that has a pArticles field with a value greater than 90.

filter_none

edit
close

play_arrow

link
brightness_4
code

db.contributor.updateOne({name: "Sumit", articles: {$elemMatch:
                                          {pArticles: {$gt: 30}}}},
                             {$set: {"articles.$.tArticles": 200}})

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.