- Reference >
- Query, Update, Projection, and Aggregation Operators >
- $ (update)
$ (update)¶
-
$
¶ Syntax:
{ "<array>.$" : value }
The positional
$
operator identifies an element in anarray
field 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
array
field must appear as part of thequery document
.
Consider a collection
students
with the following documents:To update
80
to82
in thegrades
array 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
students
collection whosegrades
field value is an array of embedded documents:Use the positional
$
operator to update the value of thestd
field in the embedded document with thegrade
of85
:Note
- the positional