@@ -7,25 +7,24 @@ Map-Reduce Examples
77 .. admonition :: Aggregation Pipeline as Alternative
88 :class: note
99
10- :doc: `Aggregation pipeline </core/aggregation-pipeline >`
11- provides better performance and a more coherent interface than
12- map-reduce, and various map-reduce expressions can be
13- rewritten using :doc: `aggregation pipeline operators
14- </meta/aggregation-quick-reference>`, such as :pipeline: `$group `,
15- :pipeline: `$merge `, etc.
10+ :doc: `Aggregation pipeline </core/aggregation-pipeline >` provides
11+ better performance and a simpler interface than map-reduce, and
12+ map-reduce expressions can be rewritten using :doc: `aggregation
13+ pipeline operators </meta/aggregation-quick-reference>` such as
14+ :pipeline: `$group `, :pipeline: `$merge `, and others.
1615
17- For map-reduce expressions that require custom functionality,
18- MongoDB provides the :group: `$accumulator ` and
19- :expression: ` $function ` aggregation operators starting in version
20- 4.4. These operators provide users with the ability to define custom
21- aggregation expressions in JavaScript.
16+ For map-reduce expressions that require custom functionality, MongoDB
17+ provides the :group: `$accumulator ` and :expression: ` $function `
18+ aggregation operators starting in version 4.4. These operators
19+ provide the ability to define custom aggregation expressions in
20+ JavaScript.
2221
23- The example below includes aggregation pipeline alternatives without
24- custom aggregation expressions. For alternatives that use custom
25- expressions, see :ref: `Map-Reduce to Aggregation Pipeline
26- Translation Examples <mr-to-agg-examples>`.
22+ The examples in this section include aggregation pipeline
23+ alternatives without custom aggregation expressions. For alternatives
24+ that use custom expressions, see :ref: `Map-Reduce to Aggregation
25+ Pipeline Translation Examples <mr-to-agg-examples>`.
2726
28- Create a sample collection ``orders `` with the following documents:
27+ Create a sample collection ``orders `` with these documents:
2928
3029.. code-block :: javascript
3130
@@ -61,7 +60,7 @@ by the ``cust_id``, and calculate the sum of the ``price`` for each
6160 map-reduce operation is processing.
6261
6362 - The function maps the ``price `` to the ``cust_id `` for each
64- document and emits the ``cust_id `` and ``price `` pair .
63+ document and emits the ``cust_id `` and ``price ``.
6564
6665 .. code-block :: javascript
6766
@@ -88,7 +87,7 @@ by the ``cust_id``, and calculate the sum of the ``price`` for each
8887
8988 #. Perform map-reduce on all documents in the ``orders `` collection
9089 using the ``mapFunction1 `` map function and the ``reduceFunction1 ``
91- reduce function.
90+ reduce function:
9291
9392 .. code-block :: javascript
9493
@@ -109,7 +108,7 @@ by the ``cust_id``, and calculate the sum of the ``price`` for each
109108
110109 db .map_reduce_example .find ().sort ( { _id: 1 } )
111110
112- The operation returns the following documents:
111+ The operation returns these documents:
113112
114113 .. code-block :: javascript
115114 : copyable: false
@@ -135,10 +134,10 @@ Aggregation Alternative
135134 ])
136135
137136 #. The :pipeline: `$group ` stage groups by the ``cust_id `` and
138- calculates the ``value `` field (See also :expression: `$sum `) . The
137+ calculates the ``value `` field using :expression: `$sum `. The
139138 ``value `` field contains the total ``price `` for each ``cust_id ``.
140139
141- The stage output the following documents to the next stage:
140+ This stage outputs these documents to the next stage:
142141
143142 .. code-block :: javascript
144143 : copyable: false
@@ -158,7 +157,7 @@ Aggregation Alternative
158157
159158 db .agg_alternative_1 .find ().sort ( { _id: 1 } )
160159
161- The operation returns the following documents:
160+ The operation returns these documents:
162161
163162 .. code-block :: javascript
164163 : copyable: false
@@ -181,25 +180,34 @@ Calculate Order and Total Quantity with Average Quantity Per Item
181180
182181.. map-reduce-counts-begin
183182
184- In this example, you will perform a map-reduce operation on the
183+ In the following example, you will see a map-reduce operation on the
185184``orders `` collection for all documents that have an ``ord_date `` value
186- greater than or equal to ``2020-03-01 ``. The operation groups by the
187- ``item.sku `` field, and calculates the number of orders and the total
188- quantity ordered for each ``sku ``. The operation then calculates the
189- average quantity per order for each ``sku `` value and merges the
190- results into the output collection. When merging results, if an
191- existing document has the same key as the new result, the operation
192- overwrites the existing document. If there is no existing document with
193- the same key, the operation inserts the document.
185+ greater than or equal to ``2020-03-01 ``.
186+
187+ The operation in the example:
188+
189+ #. Groups by the ``item.sku `` field, and calculates the number of orders
190+ and the total quantity ordered for each ``sku ``.
191+
192+ #. Calculates the average quantity per order for each ``sku `` value and
193+ merges the results into the output collection.
194+
195+ When merging results, if an existing document has the same key as the
196+ new result, the operation overwrites the existing document. If there is
197+ no existing document with the same key, the operation inserts the
198+ document.
199+
200+ Example steps:
194201
195202#. Define the map function to process each input document:
196203
197204 - In the function, ``this `` refers to the document that the
198205 map-reduce operation is processing.
199206
200207 - For each item, the function associates the ``sku `` with a new
201- object ``value `` that contains the ``count `` of ``1 `` and the
202- item ``qty `` for the order and emits the ``sku `` and ``value `` pair.
208+ object ``value `` that contains the ``count `` of ``1 `` and the item
209+ ``qty `` for the order and emits the ``sku `` (stored in the ``key ``)
210+ and the ``value ``.
203211
204212 .. code-block :: javascript
205213
@@ -255,7 +263,7 @@ the same key, the operation inserts the document.
255263
256264 #. Perform the map-reduce operation on the ``orders `` collection using
257265 the ``mapFunction2 ``, ``reduceFunction2 ``, and
258- ``finalizeFunction2 `` functions.
266+ ``finalizeFunction2 `` functions:
259267
260268 .. code-block :: javascript
261269
@@ -271,7 +279,7 @@ the same key, the operation inserts the document.
271279
272280 This operation uses the ``query `` field to select only those
273281 documents with ``ord_date `` greater than or equal to ``new
274- Date("2020-03-01") ``. Then it output the results to a collection
282+ Date("2020-03-01") ``. Then it outputs the results to a collection
275283 ``map_reduce_example2 ``.
276284
277285 If the ``map_reduce_example2 `` collection already exists, the
@@ -287,15 +295,15 @@ the same key, the operation inserts the document.
287295
288296 db .map_reduce_example2 .find ().sort ( { _id: 1 } )
289297
290- The operation returns the following documents:
298+ The operation returns these documents:
291299
292300 .. code-block :: javascript
293301 : copyable: false
294302
295- { " _id" : " apples" , " value" : { " count" : 3 , " qty" : 30 , " avg" : 10 } }
303+ { " _id" : " apples" , " value" : { " count" : 4 , " qty" : 35 , " avg" : 8.75 } }
296304 { " _id" : " carrots" , " value" : { " count" : 2 , " qty" : 15 , " avg" : 7.5 } }
297305 { " _id" : " chocolates" , " value" : { " count" : 3 , " qty" : 15 , " avg" : 5 } }
298- { " _id" : " oranges" , " value" : { " count" : 6 , " qty" : 58 , " avg" : 9.666666666666666 } }
306+ { " _id" : " oranges" , " value" : { " count" : 7 , " qty" : 63 , " avg" : 9 } }
299307 { " _id" : " pears" , " value" : { " count" : 1 , " qty" : 10 , " avg" : 10 } }
300308
301309 Aggregation Alternative
@@ -340,11 +348,12 @@ Aggregation Alternative
340348 #. The :pipeline: `$group ` stage groups by the ``items.sku ``, calculating for each sku:
341349
342350 - The ``qty `` field. The ``qty `` field contains the
343- total ``qty `` ordered per each ``items.sku `` (See :expression: `$sum `).
351+ total ``qty `` ordered per each ``items.sku `` using
352+ :expression: `$sum `.
344353
345354 - The ``orders_ids `` array. The ``orders_ids `` field contains an
346- array of distinct order ``_id ``'s for the ``items.sku `` (See
347- :expression: `$addToSet `) .
355+ array of distinct order ``_id ``'s for the ``items.sku `` using
356+ :expression: `$addToSet `.
348357
349358 .. code-block :: javascript
350359 : copyable: false
@@ -359,11 +368,13 @@ Aggregation Alternative
359368 mirror the map-reduce's output to have two fields ``_id `` and
360369 ``value ``. The :pipeline: `$project ` sets:
361370
362- - the ``value.count `` to the size of the ``orders_ids `` array. (See :expression: `$size `.)
371+ - the ``value.count `` to the size of the ``orders_ids `` array
372+ using :expression: `$size `.
363373
364374 - the ``value.qty `` to the ``qty `` field of input document.
365375
366- - the ``value.avg `` to the average number of qty per order. (See :expression: `$divide ` and :expression: `$size `.)
376+ - the ``value.avg `` to the average number of qty per order
377+ using :expression: `$divide ` and :expression: `$size `.
367378
368379 .. code-block :: javascript
369380 : copyable: false
@@ -386,7 +397,7 @@ Aggregation Alternative
386397
387398 db .agg_alternative_3 .find ().sort ( { _id: 1 } )
388399
389- The operation returns the following documents:
400+ The operation returns these documents:
390401
391402 .. code-block :: javascript
392403 : copyable: false
0 commit comments