@@ -111,6 +111,48 @@ The following example appends ``89`` to the ``scores`` array:
111
111
112
112
.. _example-push-each:
113
113
114
+ Append a Value to Arrays in Multiple Documents
115
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116
+
117
+ Add the following documents to the ``students`` collection:
118
+
119
+ .. code-block:: javascript
120
+
121
+ db.students.insertMany( [
122
+ { _id: 2, scores: [ 45, 78, 38, 80, 89 ] } ,
123
+ { _id: 3, scores: [ 46, 78, 38, 80, 89 ] } ,
124
+ { _id: 4, scores: [ 47, 78, 38, 80, 89 ] }
125
+ ] )
126
+
127
+ The following :update:`$push` operation appends ``95`` to the
128
+ ``scores`` array in each document:
129
+
130
+ .. code-block:: javascript
131
+
132
+ db.students.updateMany(
133
+ { },
134
+ { $push: { scores: 95 } }
135
+ )
136
+
137
+ To confirm that each ``scores`` array includes ``95``, run the following
138
+ operation:
139
+
140
+ .. code-block:: javascript
141
+
142
+ db.students.find()
143
+
144
+ The operation returns the following results:
145
+
146
+ .. code-block:: javascript
147
+ :copyable: false
148
+
149
+ [
150
+ { _id: 1, scores: [ 44, 78, 38, 80, 89, 95 ] },
151
+ { _id: 2, scores: [ 45, 78, 38, 80, 89, 95 ] },
152
+ { _id: 3, scores: [ 46, 78, 38, 80, 89, 95 ] },
153
+ { _id: 4, scores: [ 47, 78, 38, 80, 89, 95 ] }
154
+ ]
155
+
114
156
Append Multiple Values to an Array
115
157
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116
158
@@ -127,4 +169,5 @@ Use ``$push`` Operator with Multiple Modifiers
127
169
.. seealso::
128
170
129
171
- :method:`db.collection.update()`
172
+ - :method:`db.collection.updateMany()`
130
173
- :method:`db.collection.findAndModify()`
0 commit comments