@@ -121,6 +121,48 @@ Example output:
121
121
122
122
.. _example-push-each:
123
123
124
+ Append a Value to Arrays in Multiple Documents
125
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126
+
127
+ Add the following documents to the ``students`` collection:
128
+
129
+ .. code-block:: javascript
130
+
131
+ db.students.insertMany( [
132
+ { _id: 2, scores: [ 45, 78, 38, 80, 89 ] } ,
133
+ { _id: 3, scores: [ 46, 78, 38, 80, 89 ] } ,
134
+ { _id: 4, scores: [ 47, 78, 38, 80, 89 ] }
135
+ ] )
136
+
137
+ The following :update:`$push` operation appends ``95`` to the
138
+ ``scores`` array in each document:
139
+
140
+ .. code-block:: javascript
141
+
142
+ db.students.updateMany(
143
+ { },
144
+ { $push: { scores: 95 } }
145
+ )
146
+
147
+ To confirm that each ``scores`` array includes ``95``, run the following
148
+ operation:
149
+
150
+ .. code-block:: javascript
151
+
152
+ db.students.find()
153
+
154
+ The operation returns the following results:
155
+
156
+ .. code-block:: javascript
157
+ :copyable: false
158
+
159
+ [
160
+ { _id: 1, scores: [ 44, 78, 38, 80, 89, 95 ] },
161
+ { _id: 2, scores: [ 45, 78, 38, 80, 89, 95 ] },
162
+ { _id: 3, scores: [ 46, 78, 38, 80, 89, 95 ] },
163
+ { _id: 4, scores: [ 47, 78, 38, 80, 89, 95 ] }
164
+ ]
165
+
124
166
Append Multiple Values to an Array
125
167
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126
168
@@ -137,4 +179,4 @@ Use ``$push`` Operator with Multiple Modifiers
137
179
.. seealso::
138
180
139
181
- :method:`db.collection.updateMany()`
140
- - :method:`db.collection.findAndModify()`
182
+ - :method:`db.collection.findAndModify()`
0 commit comments