- Reference >
- Query, Update, Projection, and Aggregation Operators >
- $ (update)
$ (update)¶
-
$¶ Syntax:
{ "<array>.$" : value }The positional
$operator identifies an element in anarrayfield to update without explicitly specifying the position of the element in the array. To project, or return, an array element from a read operation, see the$projection operator.When used with the
update()method,- the positional
$operator acts as a placeholder for the first element that matches the query document, and - the
arrayfield must appear as part of thequery document.
Consider a collection
studentswith the following documents:To update
80to82in thegradesarray in the first document, use the positional$operator if you do not know the position of the element in the array:Remember that the positional
$operator acts as a placeholder for the first match of the update query document.The positional
$operator facilitates updates to arrays that contain embedded documents. Use the positional$operator to access the fields in the embedded documents with the dot notation on the$operator.Consider the following document in the
studentscollection whosegradesfield value is an array of embedded documents:Use the positional
$operator to update the value of thestdfield in the embedded document with thegradeof85:Note
- the positional