MongoDB Map Reduce Examples

Let’s look at some examples of MongoDB map reduce function.

In this example, we are working with:

Database: w3wiki2
Collection: employee
Documents: Six documents that contains the details of the employees

Find the sum of ranks grouped by ages

Here, we will calculate the sum of rank present inside the particular age group. Now age is our key on which we will perform group by (like in MySQL) and rank will be the key on which we will perform sum aggregation.

Query:

var map=function(){ emit(this.age,this.rank)};
var reduce=function(age,rank){ return Array.sum(rank);};
db.employee.mapReduce(map,reduce,{out :"resultCollection1"});
  • Inside map() function, i.e., map() : function map(){ emit(this.age,this.rank);}; we will write the emit(this.age,this.rank) function. Here this represents the current collection being iterated and the first key is age using age we will group the result like having age 24 give the sum of all rank or having age 25 give the sum of all rank and the second argument is rank on which aggregation will be performed.
  • Inside the reduce function, i.e., reduce(): function reduce(key,rank){ return Array.sum(rank); }; we will perform the aggregation function.
  • Now the third parameter will be output where we will define the collection where the result will be saved, i.e., {out :”resultCollection1″}. Here, out represents the key whose value is the collection name where the result will be saved.

Output:

Performing avg() aggregation on rank grouped by ages

In this example, we will calculate the average of the ranks grouped by age.

Query:

var map=function(){ emit(this.age,this.rank)};
var reduce=function(age,rank){ return Array.avg(rank);};
db.employee.mapReduce(map,reduce,{out :"resultCollection3"});
db.resultCollection3.find()
  • map(): Function map(){ emit(this.age, this.rank)};. Here age is the key by which we will group and rank is the key on which avg() aggregation will be performed.
  • reduce(): Function reduce (age,rank){ return Array.avg(rank)l};
  • output: {out:”resultCollection3″}

Output:

MongoDB Map-Reduce

MongoDB Map-Reduce is a data processing programming model that helps to perform operations on large data sets and produce aggregated results. MongoDB provides the mapReduce() function to perform the map-reduce operations. This function has two main functions, i.e., map function and reduce function.

The map function is used to group all the data based on the key-value and the reduce function is used to perform operations on the mapped data. So, the data is independently mapped and reduced in different spaces and then combined in the function and the result will be saved to the specified new collection.

This mapReduce() function generally operates on large data sets. Using Map Reduce you can perform aggregation operations such as max, avg on the data using some key and it is similar to groupBy in SQL. It performs on data independently and in parallel.

Similar Reads

Syntax

db.collectionName.mapReduce(... map(),...reduce(),...query{},...output{});...

Steps to use Map Reduce in MongoDB

Look at this step-by-step guide to learn how to use MongoDB Map-Reduce. Let’s try to understand the mapReduce() using the following example:...

MongoDB Map Reduce Examples

Let’s look at some examples of MongoDB map reduce function....

When to use Map-Reduce in MongoDB?

In MongoDB, you can use Map-reduce when your aggregation query is slow because data is present in a large amount and the aggregation query is taking more time to process. So using map-reduce you can perform action faster on large datasets than aggregation query....

MongoDB Map Reduce -FAQs

What is map reducing in MongoDB?...

Contact Us