@@ -24,14 +24,14 @@ Definition
24
24
25
25
{ <update operator>: { "<array>.$[]" : value } }
26
26
27
- Use in update operations, e.g. :method:`db.collection.update ()` and
28
- :method:`db.collection.findAndModify()`, to modify all array
27
+ Use in update operations, e.g. :method:`db.collection.updateOne ()`
28
+ and :method:`db.collection.findAndModify()`, to modify all array
29
29
elements for the document or documents that match the query
30
30
condition. For example:
31
31
32
32
.. code-block:: javascript
33
33
34
- db.collection.updateMany (
34
+ db.collection.updateOne (
35
35
{ <query conditions> },
36
36
{ <update operator>: { "<array>.$[]" : value } }
37
37
)
@@ -57,7 +57,7 @@ array field:
57
57
58
58
.. code-block:: javascript
59
59
60
- db.collection.update (
60
+ db.collection.updateOne (
61
61
{ myArray: [ 5, 8 ] },
62
62
{ $set: { "myArray.$[]": 10 } },
63
63
{ upsert: true }
@@ -79,23 +79,23 @@ documents were found to update:
79
79
80
80
.. code-block:: javascript
81
81
82
- db.collection.update (
83
- { myArray: 5 },
82
+ db.emptyCollection.updateOne (
83
+ { },
84
84
{ $set: { "myArray.$[]": 10 } },
85
85
{ upsert: true }
86
86
)
87
-
88
- db.collection.update (
89
- { },
87
+
88
+ db.emptyCollection.updateOne (
89
+ { myArray: 5 },
90
90
{ $set: { "myArray.$[]": 10 } },
91
91
{ upsert: true }
92
92
)
93
93
94
94
Nested Arrays
95
95
~~~~~~~~~~~~~
96
96
97
- The ``$[]`` operator can be used for queries which
98
- traverse more than one array and nested arrays.
97
+ The ``$[]`` operator can be used for queries that traverse more than
98
+ one array and nested arrays.
99
99
100
100
For an example, see :ref:`position-nested-arrays`.
101
101
@@ -107,28 +107,29 @@ Examples
107
107
Update All Elements in an Array
108
108
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109
109
110
- Consider a collection ``students`` with the following documents :
110
+ Create the ``students`` collection :
111
111
112
112
.. code-block:: javascript
113
113
114
- { "_id" : 1, "grades" : [ 85, 82, 80 ] }
115
- { "_id" : 2, "grades" : [ 88, 90, 92 ] }
116
- { "_id" : 3, "grades" : [ 85, 100, 90 ] }
114
+ db.students.insertMany( [
115
+ { "_id" : 1, "grades" : [ 85, 82, 80 ] },
116
+ { "_id" : 2, "grades" : [ 88, 90, 92 ] },
117
+ { "_id" : 3, "grades" : [ 85, 100, 90 ] }
118
+ ] )
117
119
118
120
To increment all elements in the ``grades`` array by ``10`` for all
119
121
documents in the collection, use the all positional :update:`$[]`
120
122
operator:
121
123
122
124
.. code-block:: javascript
123
125
124
- db.students.update (
126
+ db.students.updateMany (
125
127
{ },
126
128
{ $inc: { "grades.$[]": 10 } },
127
- { multi: true }
128
129
)
129
130
130
- The all positional :update:`$[]` operator acts as a
131
- placeholder for all elements in the array field.
131
+ The all positional :update:`$[]` operator acts as a placeholder for all
132
+ elements in the array field.
132
133
133
134
After the operation, the ``students`` collection contains the following
134
135
documents:
@@ -143,48 +144,49 @@ Update All Documents in an Array
143
144
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144
145
145
146
The :update:`$[]` positional operator facilitates updates to arrays
146
- that contain embedded documents. To access the fields
147
- in the embedded documents, use the :ref:`dot notation
148
- <document-dot-notation>` on the :update:`$[]` operator.
147
+ that contain embedded documents. To access the fields in the embedded
148
+ documents, use the :ref:`dot notation <document-dot-notation>` with the
149
+ :update:`$[]` operator.
149
150
150
151
.. code-block:: javascript
151
152
152
- db.collection.update (
153
+ db.collection.updateOne (
153
154
{ <query selector> },
154
155
{ <update operator>: { "array.$[].field" : value } }
155
156
)
156
157
157
158
158
- Consider a collection ``students2`` with the following documents :
159
+ Create the ``students2`` collection :
159
160
160
161
.. code-block:: javascript
161
162
162
- {
163
- "_id" : 1,
164
- "grades" : [
165
- { "grade" : 80, "mean" : 75, "std" : 8 },
166
- { "grade" : 85, "mean" : 90, "std" : 6 },
167
- { "grade" : 85, "mean" : 85, "std" : 8 }
168
- ]
169
- }
170
- {
171
- "_id" : 2,
172
- "grades" : [
173
- { "grade" : 90, "mean" : 75, "std" : 8 },
174
- { "grade" : 87, "mean" : 90, "std" : 5 },
175
- { "grade" : 85, "mean" : 85, "std" : 6 }
176
- ]
177
- }
163
+ db.students2.insertMany( [
164
+ {
165
+ "_id" : 1,
166
+ "grades" : [
167
+ { "grade" : 80, "mean" : 75, "std" : 8 },
168
+ { "grade" : 85, "mean" : 90, "std" : 6 },
169
+ { "grade" : 85, "mean" : 85, "std" : 8 }
170
+ ]
171
+ },
172
+ {
173
+ "_id" : 2,
174
+ "grades" : [
175
+ { "grade" : 90, "mean" : 75, "std" : 8 },
176
+ { "grade" : 87, "mean" : 90, "std" : 5 },
177
+ { "grade" : 85, "mean" : 85, "std" : 6 }
178
+ ]
179
+ }
180
+ ] )
178
181
179
182
To modify the value of the ``std`` field for all elements in the
180
183
``grades`` array, use the positional :update:`$[]` operator:
181
184
182
185
.. code-block:: javascript
183
186
184
- db.students2.update (
187
+ db.students2.updateMany (
185
188
{ },
186
189
{ $inc: { "grades.$[].std" : -2 } },
187
- { multi: true }
188
190
)
189
191
190
192
After the operation, the collection has the following documents:
@@ -211,24 +213,25 @@ After the operation, the collection has the following documents:
211
213
Update Arrays Specified Using a Negation Query Operator
212
214
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
213
215
214
- Consider a collection ``results`` with the following documents :
216
+ Create the ``results`` collection :
215
217
216
218
.. code-block:: javascript
217
219
218
- { "_id" : 1, "grades" : [ 85, 82, 80 ] }
219
- { "_id" : 2, "grades" : [ 88, 90, 92 ] }
220
- { "_id" : 3, "grades" : [ 85, 100, 90 ] }
220
+ db.results.insertMany( [
221
+ { "_id" : 1, "grades" : [ 85, 82, 80 ] },
222
+ { "_id" : 2, "grades" : [ 88, 90, 92 ] },
223
+ { "_id" : 3, "grades" : [ 85, 100, 90 ] }
224
+ ] )
221
225
222
226
To increment all elements in the ``grades`` array by ``10`` for all
223
227
documents **except** those with the value ``100`` in the ``grades``
224
228
array, use the all positional :update:`$[]` operator:
225
229
226
230
.. code-block:: javascript
227
231
228
- db.results.update (
232
+ db.results.updateMany (
229
233
{ "grades" : { $ne: 100 } },
230
234
{ $inc: { "grades.$[]": 10 } },
231
- { multi: true }
232
235
)
233
236
234
237
The all positional :update:`$[]` operator acts as a
@@ -275,7 +278,7 @@ nested ``grades.questions`` array, regardless of ``type``:
275
278
db.students3.updateMany(
276
279
{},
277
280
{ $inc: { "grades.$[].questions.$[score]": 2 } },
278
- { arrayFilters: [ { "score": { $gte: 8 } } ], multi: true }
281
+ { arrayFilters: [ { "score": { $gte: 8 } } ] }
279
282
)
280
283
281
284
The updated documents look like this:
0 commit comments