- Aggregation >
- Aggregation Examples >
- 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:
Consider the following map-reduce operations on a collection
orders that contains documents of the following prototype:
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,
thisrefers to the document that the map-reduce operation is processing. - The function maps the
priceto thecust_idfor each document and emits thecust_idandpricepair.
- In the function,
Define the corresponding reduce function with two arguments
keyCustIdandvaluesPrices:- The
valuesPricesis an array whose elements are thepricevalues emitted by the map function and grouped bykeyCustId. - The function reduces the
valuesPricearray to the sum of its elements.
- The
Perform the map-reduce on all documents in the
orderscollection using themapFunction1map function and thereduceFunction1reduce function.This operation outputs the results to a collection named
map_reduce_example. If themap_reduce_examplecollection already exists, the operation will replace the contents with the results of this map-reduce operation:
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 01/01/2012. The operation groups by the
item.sku field, and calculates the number of
orders and the total quantity ordered for each sku. The operation concludes by
calculating the average quantity per order for each sku value:
Define the map function to process each input document:
- In the function,
thisrefers to the document that the map-reduce operation is processing. - For each item, the function associates the
skuwith a new objectvaluethat contains thecountof1and the itemqtyfor the order and emits theskuandvaluepair.
- In the function,
Define the corresponding reduce function with two arguments
keySKUandcountObjVals:countObjValsis an array whose elements are the objects mapped to the groupedkeySKUvalues passed by map function to the reducer function.- The function reduces the
countObjValsarray to a single objectreducedValuethat contains thecountand theqtyfields. - In
reducedVal, thecountfield contains the sum of thecountfields from the individual array elements, and theqtyfield contains the sum of theqtyfields from the individual array elements.
Define a finalize function with two arguments
keyandreducedVal. The function modifies thereducedValobject to add a computed field namedavgand returns the modified object:Perform the map-reduce operation on the
orderscollection using themapFunction2,reduceFunction2, andfinalizeFunction2functions.This operation uses the
queryfield to select only those documents withord_dategreater thannew Date(01/01/2012). Then it output the results to a collectionmap_reduce_example. If themap_reduce_examplecollection already exists, the operation will merge the existing contents with the results of this map-reduce operation.