Skip to content

Commit 40fdbba

Browse files
authored
DOCSP 22373 - adding updateMany() example to push (#1455) (#1470)
* 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 19566b0 commit 40fdbba

File tree

1 file changed

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

1 file changed

+43
-0
lines changed

source/reference/operator/update/push.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,48 @@ The following example appends ``89`` to the ``scores`` array:
111111

112112
.. _example-push-each:
113113

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+
114156
Append Multiple Values to an Array
115157
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116158

@@ -127,4 +169,5 @@ Use ``$push`` Operator with Multiple Modifiers
127169
.. seealso::
128170

129171
- :method:`db.collection.update()`
172+
- :method:`db.collection.updateMany()`
130173
- :method:`db.collection.findAndModify()`

0 commit comments

Comments
 (0)