Skip to content

Commit 5ad3ca6

Browse files
authored
DOCSP 21855 clarifying/updating updateMany and set (aggregation) examples (#1507)
* 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 0c53309 commit 5ad3ca6

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
@@ -395,8 +395,8 @@ Using the aggregation pipeline allows for a more expressive update
395395
statement, such as expressing conditional updates based on current
396396
field values or updating one field using the value of another field(s).
397397

398-
Example 1
399-
`````````
398+
Example 1: Update with Aggregation Pipeline Using Existing Fields
399+
`````````````````````````````````````````````````````````````````
400400

401401
The following examples uses the aggregation pipeline to modify a field
402402
using the values of the other fields in the document.
@@ -458,8 +458,8 @@ After the command, the collection contains the following documents:
458458
{ "_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" ] }
459459
{ "_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" ] }
460460

461-
Example 2
462-
`````````
461+
Example 2: Update with Aggregation Pipeline Using Existing Fields Conditionally
462+
```````````````````````````````````````````````````````````````````````````````
463463

464464
The aggregation pipeline allows the update to perform conditional
465465
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)