Skip to content

Commit bc8168b

Browse files
committed
DOCS-503 positional operator doc
1 parent 1c1a23a commit bc8168b

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
=
2+
$
3+
=
4+
5+
.. default-domain:: mongodb
6+
7+
.. operator:: $
8+
9+
*Syntax*: ``{ "array.$" : value }``
10+
11+
The positional :operator:`$` operator identifies an element in an
12+
``array`` field to update without explicitly specifying the position
13+
of the element in the array. The positional :operator:`$` operator
14+
is used in conjunction with the :method:`update()
15+
<db.collection.update()>` method and acts as a placeholder for the
16+
**first match** of the update ``query selector``:
17+
18+
.. code-block:: javascript
19+
20+
db.collection.update( { <query selector> }, { <update operator>: { "array.$" : value } } )
21+
22+
The ``array`` field **must** appear as part of the ``query selector``.
23+
24+
Consider the following collection ``students`` with the following documents:
25+
26+
.. code-block:: javascript
27+
28+
{ "_id" : 1, "grades" : [ 80, 85, 90 ] }
29+
{ "_id" : 2, "grades" : [ 88, 90, 92 ] }
30+
{ "_id" : 3, "grades" : [ 85, 100, 90 ] }
31+
32+
To update ``80`` to ``82`` in the ``grades`` array field in the
33+
first document, you can use the positional :operator:`$` operator if
34+
you did not know the position of the element in the array:
35+
36+
.. code-block:: javascript
37+
38+
db.students.update( { _id: 1, grades: 80 }, { $set: { "grades.$" : 82 } } )
39+
40+
Remember that the positional :operator:`$` operator acts as a
41+
placeholder for the **first match** of the update ``query selector``.
42+
43+
The positional :operator:`$` operator facilitates updates to arrays
44+
that contain embedded documents. You can access the fields in the
45+
embedded documents by applying the dot-notation on the :operator:`$`
46+
operator.
47+
48+
.. code-block:: javascript
49+
50+
db.collection.update( { <query selector> }, { <update operator>: { "array.$.field" : value } } )
51+
52+
Consider the following document in the ``students`` collection whose
53+
``grades`` field value is an array of embedded documents:
54+
55+
.. code-block:: javascript
56+
57+
{ "_id" : 4, "grades" : [ { grade: 80, mean: 75, std: 8 },
58+
{ grade: 85, mean: 90, std: 5 },
59+
{ grade: 90, mean: 85, std: 3 } ] }
60+
61+
To update the ``std`` value of the embedded document with the
62+
``grade`` of ``85``, you can use the positional :operator:`$`
63+
operator:
64+
65+
.. code-block:: javascript
66+
67+
db.students.update( { _id: 4, "grades.grade": 85 }, { $set: { "grades.$.std" : 6 } } )
68+
69+
Consider the following behaviors when using the positional
70+
:operator:`$` operator:
71+
72+
- The positional :operator:`$` operator should not be used with an
73+
:term:`upsert` since an insert will use the ``$`` as a field name
74+
in the inserted document.
75+
76+
- When used with the :operator:`$unset` operator, the positional
77+
:operator:`$` operator does not remove the matching element from
78+
the array but rather set it to ``null``.
79+
80+
.. seealso::
81+
82+
:method:`update() <db.collection.update()>`, :operator:`$set`.

source/reference/operators.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ Fields
187187
Array
188188
~~~~~
189189

190+
.. include:: operator/positional.txt
191+
:start-after: mongodb
192+
190193
.. include:: operator/push.txt
191194
:start-after: mongodb
192195

0 commit comments

Comments
 (0)