MapReduce in MongoDB

Shobhit Singh Pal
3 min readMay 14, 2021

Aggregation operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods.

Aggregation: Map-Reduce function

MongoDB applies the map phase or map function to each input document. The map function emits key-value pairs. For those keys that have multiple values, MongoDB applies the reduce phase or reduce function, which collects and condenses the aggregated data. MongoDB then stores the results in a collection.

Syntax of mapReduce command:

db.runCommand( {
mapReduce: <string>,
map: <string or JavaScript>,
reduce: <string or JavaScript>,
finalize: <string or JavaScript>,
out: <output>,
query: <document>,
sort: <document>,
limit: <number>,
scope: <document>,
jsMode: <boolean>,
verbose: <boolean>,
bypassDocumentValidation: <boolean>,
collation: <document>,
writeConcern: <document>,
comment: <any>
} )

Here it is compulsory to pass the arguments to mapReduce, map, reduce and out parameters. The mapReduce, map, reduce and out parameters take collection , map function, reduce function and collection name as arguments respectively.

Syntax of Map Function

function() {
...
emit(key, value);
}
  • In the map function, reference the current document as this within the function.

The map function may optionally call emit(key, value) any number of times to create an output document associating key with value.

Syntax of Reduce function

function(key, values) {
...
return result;
}

Method: db.collection.mapReduce

The db.collection.mapReduce method is a wrapper around the mapReduce command.

db.<collection_name>.mapReduce(
mapfunction;
Reducefunction;
{ out: "<collection_name>" }
)

Here db is the current working database in the shell.

Demonstration:

Overview of the dataset:

MapReduce program

The map function maps Store field values as keys to the values of Weekly_Sales field as values. The reduce function collects the Key values and sum up the list of each key values. weekly_sales_total is the name of the output collection. weekly_sales_total is the weekly sales total as per store number.

Running in MongoDB Shell

Similarly created some more Map-Reduce program:

  • The below MapReduce program calculates the weekly sales as per holiday in the week.

The true says when there was holiday in the week and false says there was no holiday in the week.

Many weeks have no holiday still the sales is quite comparable which means that when there is holiday in the week the sales is more.

  • Calculates weekly sales as per fuel price.

Hope you get an idea of MapReduce function in MongoDB.

Below is the link for dataset

--

--