- Aggregation >
- Map-Reduce >
- Map-Reduce Examples
Map-Reduce Examples¶
On this page
In the mongo
shell, the db.collection.mapReduce()
method is a wrapper around the mapReduce
command. The
following examples use the db.collection.mapReduce()
method:
Aggregation Pipeline as Alternative
Aggregation pipeline
provides better performance and a more coherent interface than
map-reduce, and various map-reduce expressions can be
rewritten using aggregation pipeline operators, such as $group
,
$merge
, etc.
For map-reduce expressions that require custom functionality,
MongoDB provides the $accumulator
and
$function
aggregation operators starting in version
4.4. These operators provide users with the ability to define custom
aggregation expressions in JavaScript.
The example below includes aggregation pipeline alternatives without custom aggregation expressions. For alternatives that use custom expressions, see Map-Reduce to Aggregation Pipeline Translation Examples.
Create a sample collection orders
with the following documents:
Return the Total Price Per Customer¶
Perform the map-reduce operation on the orders
collection to group
by the cust_id
, and calculate the sum of the price
for each
cust_id
:
Define the map function to process each input document:
- In the function,
this
refers to the document that the map-reduce operation is processing. - The function maps the
price
to thecust_id
for each document and emits thecust_id
andprice
pair.
- In the function,
Define the corresponding reduce function with two arguments
keyCustId
andvaluesPrices
:- The
valuesPrices
is an array whose elements are theprice
values emitted by the map function and grouped bykeyCustId
. - The function reduces the
valuesPrice
array to the sum of its elements.
- The
Perform map-reduce on all documents in the
orders
collection using themapFunction1
map function and thereduceFunction1
reduce function.This operation outputs the results to a collection named
map_reduce_example
. If themap_reduce_example
collection already exists, the operation will replace the contents with the results of this map-reduce operation.Query the
map_reduce_example
collection to verify the results:The operation returns the following documents:
Aggregation Alternative¶
Using the available aggregation pipeline operators, you can rewrite the map-reduce operation without defining custom functions:
The
$group
stage groups by thecust_id
and calculates thevalue
field (See also$sum
). Thevalue
field contains the totalprice
for eachcust_id
.The stage output the following documents to the next stage:
Then, the
$out
writes the output to the collectionagg_alternative_1
. Alternatively, you could use$merge
instead of$out
.Query the
agg_alternative_1
collection to verify the results:The operation returns the following documents:
See also
For an alternative that uses custom aggregation expressions, see Map-Reduce to Aggregation Pipeline Translation Examples.
Calculate Order and Total Quantity with Average Quantity Per Item¶
In this example, you will perform a map-reduce operation on the
orders
collection for all documents that have an ord_date
value
greater than or equal to 2020-03-01
. The operation groups by the
item.sku
field, and calculates the number of orders and the total
quantity ordered for each sku
. The operation then calculates the
average quantity per order for each sku
value and merges the
results into the output collection. When merging results, if an
existing document has the same key as the new result, the operation
overwrites the existing document. If there is no existing document with
the same key, the operation inserts the document.
Define the map function to process each input document:
- In the function,
this
refers to the document that the map-reduce operation is processing. - For each item, the function associates the
sku
with a new objectvalue
that contains thecount
of1
and the itemqty
for the order and emits thesku
andvalue
pair.
- In the function,
Define the corresponding reduce function with two arguments
keySKU
andcountObjVals
:countObjVals
is an array whose elements are the objects mapped to the groupedkeySKU
values passed by map function to the reducer function.- The function reduces the
countObjVals
array to a single objectreducedValue
that contains thecount
and theqty
fields. - In
reducedVal
, thecount
field contains the sum of thecount
fields from the individual array elements, and theqty
field contains the sum of theqty
fields from the individual array elements.
Define a finalize function with two arguments
key
andreducedVal
. The function modifies thereducedVal
object to add a computed field namedavg
and returns the modified object:Perform the map-reduce operation on the
orders
collection using themapFunction2
,reduceFunction2
, andfinalizeFunction2
functions.This operation uses the
query
field to select only those documents withord_date
greater than or equal tonew Date("2020-03-01")
. Then it output the results to a collectionmap_reduce_example2
.If the
map_reduce_example2
collection already exists, the operation will merge the existing contents with the results of this map-reduce operation. That is, if an existing document has the same key as the new result, the operation overwrites the existing document. If there is no existing document with the same key, the operation inserts the document.Query the
map_reduce_example2
collection to verify the results:The operation returns the following documents:
Aggregation Alternative¶
Using the available aggregation pipeline operators, you can rewrite the map-reduce operation without defining custom functions:
The
$match
stage selects only those documents withord_date
greater than or equal tonew Date("2020-03-01")
.The
$unwinds
stage breaks down the document by theitems
array field to output a document for each array element. For example:The
$group
stage groups by theitems.sku
, calculating for each sku:- The
qty
field. Theqty
field contains the totalqty
ordered per eachitems.sku
(See$sum
). - The
orders_ids
array. Theorders_ids
field contains an array of distinct order_id
’s for theitems.sku
(See$addToSet
).
- The
The
$project
stage reshapes the output document to mirror the map-reduce’s output to have two fields_id
andvalue
. The$project
sets:Finally, the
$merge
writes the output to the collectionagg_alternative_3
. If an existing document has the same key_id
as the new result, the operation overwrites the existing document. If there is no existing document with the same key, the operation inserts the document.Query the
agg_alternative_3
collection to verify the results:The operation returns the following documents:
See also
For an alternative that uses custom aggregation expressions, see Map-Reduce to Aggregation Pipeline Translation Examples.