Skip to content

Commit 7b177a0

Browse files
authored
DOCSP 21855 clarifying/updating updateMany and set (aggregation) examples (#1507) (#1565)
* DOCSP-21855 updating updateMany and set(aggregation) examples * DOCSP-21855 updating updateMany and set(aggregation) examples * DOCSP-21855 updating updateMany and set(aggregation) examples * DOCSP-21855 formatting fixes * DOCSP-21855 formatting fixes * DOCSP-21855 formatting fixes * DOCSP-21855 wording update
1 parent a8b4b13 commit 7b177a0

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

source/reference/method/db.collection.updateMany.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,8 @@ Using the aggregation pipeline allows for a more expressive update
397397
statement, such as expressing conditional updates based on current
398398
field values or updating one field using the value of another field(s).
399399

400-
Example 1
401-
`````````
400+
Example 1: Update with Aggregation Pipeline Using Existing Fields
401+
`````````````````````````````````````````````````````````````````
402402

403403
The following examples uses the aggregation pipeline to modify a field
404404
using the values of the other fields in the document.
@@ -460,8 +460,8 @@ After the command, the collection contains the following documents:
460460
{ "_id" : 1, "member" : "abc123", "status" : "Modified", "points" : 2, "lastUpdate" : ISODate("2020-01-23T05:50:49.247Z"), "comments" : [ "note to self: confirm status", "Need to activate" ] }
461461
{ "_id" : 2, "member" : "xyz123", "status" : "Modified", "points" : 60, "lastUpdate" : ISODate("2020-01-23T05:50:49.247Z"), "comments" : [ "reminder: ping me at 100pts", "Some random comment" ] }
462462

463-
Example 2
464-
`````````
463+
Example 2: Update with Aggregation Pipeline Using Existing Fields Conditionally
464+
```````````````````````````````````````````````````````````````````````````````
465465

466466
The aggregation pipeline allows the update to perform conditional
467467
updates based on the current field values as well as use current field

source/reference/operator/aggregation/set.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,52 @@ The operation returns the following:
247247
:copyable: false
248248

249249
{ "_id" : 1, "student" : "Maya", "homework" : [ 10, 5, 10, 7 ], "quiz" : [ 10, 8 ], "extraCredit" : 0 }
250+
251+
Creating a New Field with Existing Fields
252+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
253+
254+
Create a sample ``scores`` collection with the following:
255+
256+
.. code-block:: javascript
257+
258+
db.scores.insertMany([
259+
{ _id: 1, student: "Maya", homework: [ 10, 5, 10 ], quiz: [ 10, 8 ], extraCredit: 0 },
260+
{ _id: 2, student: "Ryan", homework: [ 5, 6, 5 ], quiz: [ 8, 8 ], extraCredit: 8 }
261+
])
262+
263+
The following aggregation operation adds a new field ``quizAverage``
264+
to each document that contains the average of the ``quiz`` array.
265+
266+
.. code-block:: javascript
267+
268+
db.scores.aggregate( [
269+
{
270+
$set: {
271+
quizAverage: { $avg: "$quiz" }
272+
}
273+
}
274+
] )
275+
276+
The operation returns the following documents:
277+
278+
.. code-block:: javascript
279+
:copyable: false
280+
281+
[
282+
{
283+
_id: 1,
284+
student: 'Maya',
285+
homework: [ 10, 5, 10 ],
286+
quiz: [ 10, 8 ],
287+
extraCredit: 0,
288+
quizAverage: 9
289+
},
290+
{
291+
_id: 2,
292+
student: 'Ryan',
293+
homework: [ 5, 6, 5 ],
294+
quiz: [ 8, 8 ],
295+
extraCredit: 8,
296+
quizAverage: 8
297+
}
298+
]

0 commit comments

Comments
 (0)