Open In App

MongoDB – Positional Operator ($)

Last Updated : 10 May, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.




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


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.




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


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.




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




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads