How to Use MongoDB MapReduce for Big Data

Posted on Posted in Stories

If you’re working with huge, complex data for analysis, then you’re probably wondering how you could turn that data into a single piece of information valuable for a business organization.

Here’s how MapReduce operations in MongoDB helps you do the work.

Map-reduce operations involve two job functions, the mapping and reduce operations. Let’s say we want to retrieve the gameID, gameDuration, and lastEventType from the collection where we would be getting our data.

Sample map job function:

mapAverageGameTimeByGame = function() {

emit(this.gameId, {“gameDuration” : this.gameDuration, “event” : this.lastEventType});


And then, we want to aggregate all the data that will be retrieved from the map function. Below is a sample reduce job function. This specific function will compute the average game time for all entries with event type ‘Start’ or ‘Game Complete’.

Sample reduce job function:

reduceAverageGameTimeByGame = function(key, values){
var totalPlays = 0;
var totalGameTime = 0;
    if (v.event == 'Start' || v.event == 'Game Complete'){


var averageGameTime = totalGameTime/totalPlays;
return averageGameTime;



Next, shows the command to call the map-reduce functionality in the game_stats collection (where all the appropriate data will be aggregated from). The output will be generated into the “average_game_time_by_game” collection. The average game time values will be grouped by the gameId that is collected from the map job function.

db.game_stats.mapReduce(mapAverageGameTimeByGame, reduceAverageGameTimeByGame, {out: "average_game_time_by_game"});


Putting it together, below is the command to display all the generated data from the output collection. And that should do it…



Now that you understand how MapReduce operations in MongoDB helps you do the work, your next challenge is to transform that information into knowledge useful to any organization’s business intelligence.

One thought on “How to Use MongoDB MapReduce for Big Data

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.