Skip to content

Commit b6d40b0

Browse files
authored
DOCSP 22373 - adding updateMany() example to push (#1455) (#1469)
* DOCSP-22373 adding new push example * DOCSP-22373 adding new push example * DOCSP-22373 adding new push example * DOCSP-22373 adding new push example * DOCSP-22373 adding new push example * DOCSP-22373 minor edits * DOCSP-22373 minor word change
1 parent 15f15ff commit b6d40b0

File tree

1 file changed

+43
-1
lines changed
  • source/reference/operator/update

1 file changed

+43
-1
lines changed

source/reference/operator/update/push.txt

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,48 @@ Example output:
121121

122122
.. _example-push-each:
123123

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+
124166
Append Multiple Values to an Array
125167
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126168

@@ -137,4 +179,4 @@ Use ``$push`` Operator with Multiple Modifiers
137179
.. seealso::
138180

139181
- :method:`db.collection.updateMany()`
140-
- :method:`db.collection.findAndModify()`
182+
- :method:`db.collection.findAndModify()`

0 commit comments

Comments
 (0)