@@ -24,14 +24,14 @@ Definition
2424
2525 { <update operator>: { "<array>.$[]" : value } }
2626
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
2929 elements for the document or documents that match the query
3030 condition. For example:
3131
3232 .. code-block:: javascript
3333
34- db.collection.updateMany (
34+ db.collection.updateOne (
3535 { <query conditions> },
3636 { <update operator>: { "<array>.$[]" : value } }
3737 )
@@ -57,7 +57,7 @@ array field:
5757
5858.. code-block:: javascript
5959
60- db.collection.update (
60+ db.collection.updateOne (
6161 { myArray: [ 5, 8 ] },
6262 { $set: { "myArray.$[]": 10 } },
6363 { upsert: true }
@@ -79,23 +79,23 @@ documents were found to update:
7979
8080.. code-block:: javascript
8181
82- db.collection.update (
83- { myArray: 5 },
82+ db.emptyCollection.updateOne (
83+ { },
8484 { $set: { "myArray.$[]": 10 } },
8585 { upsert: true }
8686 )
87-
88- db.collection.update (
89- { },
87+
88+ db.emptyCollection.updateOne (
89+ { myArray: 5 },
9090 { $set: { "myArray.$[]": 10 } },
9191 { upsert: true }
9292 )
9393
9494Nested Arrays
9595~~~~~~~~~~~~~
9696
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.
9999
100100For an example, see :ref:`position-nested-arrays`.
101101
@@ -107,28 +107,29 @@ Examples
107107Update All Elements in an Array
108108~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
109109
110- Consider a collection ``students`` with the following documents :
110+ Create the ``students`` collection :
111111
112112.. code-block:: javascript
113113
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+ ] )
117119
118120To increment all elements in the ``grades`` array by ``10`` for all
119121documents in the collection, use the all positional :update:`$[]`
120122operator:
121123
122124.. code-block:: javascript
123125
124- db.students.update (
126+ db.students.updateMany (
125127 { },
126128 { $inc: { "grades.$[]": 10 } },
127- { multi: true }
128129 )
129130
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.
132133
133134After the operation, the ``students`` collection contains the following
134135documents:
@@ -143,48 +144,49 @@ Update All Documents in an Array
143144~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144145
145146The :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.
149150
150151.. code-block:: javascript
151152
152- db.collection.update (
153+ db.collection.updateOne (
153154 { <query selector> },
154155 { <update operator>: { "array.$[].field" : value } }
155156 )
156157
157158
158- Consider a collection ``students2`` with the following documents :
159+ Create the ``students2`` collection :
159160
160161.. code-block:: javascript
161162
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+ ] )
178181
179182To modify the value of the ``std`` field for all elements in the
180183``grades`` array, use the positional :update:`$[]` operator:
181184
182185.. code-block:: javascript
183186
184- db.students2.update (
187+ db.students2.updateMany (
185188 { },
186189 { $inc: { "grades.$[].std" : -2 } },
187- { multi: true }
188190 )
189191
190192After the operation, the collection has the following documents:
@@ -211,24 +213,25 @@ After the operation, the collection has the following documents:
211213Update Arrays Specified Using a Negation Query Operator
212214~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
213215
214- Consider a collection ``results`` with the following documents :
216+ Create the ``results`` collection :
215217
216218.. code-block:: javascript
217219
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+ ] )
221225
222226To increment all elements in the ``grades`` array by ``10`` for all
223227documents **except** those with the value ``100`` in the ``grades``
224228array, use the all positional :update:`$[]` operator:
225229
226230.. code-block:: javascript
227231
228- db.results.update (
232+ db.results.updateMany (
229233 { "grades" : { $ne: 100 } },
230234 { $inc: { "grades.$[]": 10 } },
231- { multi: true }
232235 )
233236
234237The all positional :update:`$[]` operator acts as a
@@ -275,7 +278,7 @@ nested ``grades.questions`` array, regardless of ``type``:
275278 db.students3.updateMany(
276279 {},
277280 { $inc: { "grades.$[].questions.$[score]": 2 } },
278- { arrayFilters: [ { "score": { $gte: 8 } } ], multi: true }
281+ { arrayFilters: [ { "score": { $gte: 8 } } ] }
279282 )
280283
281284The updated documents look like this:
0 commit comments