Map Reduce function in MongoDB

You are here : Home / MongoDB Tutorial


Contents of page >

1) Map Reduce functions in MongoDB >


Map-reduce is used to process the huge volume of data.


2) How Map Reduce functions works in MongoDB?>


Map-reduce condenses large volumes of data into useful aggregated results. Let’s learn it in detail with example below.

2.1) First, Let's create new collection and insert document in it before using Map reduce function on it>
db.employee.insert({_id : 1,  firstName:"ank", salary : 1000 })
db.employee.insert({_id : 2,  firstName:"ank", salary : 2000 })
db.employee.insert({_id : 3,  firstName:"sag", salary : 3000 })
db.employee.insert({_id : 4,  firstName:"sag", salary : 4000 })
db.employee.insert({_id : 5,  firstName:"neh", salary : 5000 })
db.employee.insert({_id : 6,  firstName:"neh", salary : 5000 })
Above will create collection (or table) (if collection already exists it will insert documents in it).


2.2) FIND > Query all documents of collection using find() method>
db.employee.find()
Output>
{ "_id" : 1, "firstName" : "ank", "salary" : 1000 }
{ "_id" : 2, "firstName" : "ank", "salary" : 2000 }
{ "_id" : 3, "firstName" : "sag", "salary" : 3000 }
{ "_id" : 4, "firstName" : "sag", "salary" : 4000 }
{ "_id" : 5, "firstName" : "neh", "salary" : 5000 }
{ "_id" : 6, "firstName" : "neh", "salary" : 5000 }

3) EXAMPLE - To demonstrate mapReduce() function >


QUERY = Find sum of salary of all employee with same firstName where salary < 5000 in collection in MongoDB >


SOLUTION >
We will use mapReduce function to solve this query.
db.employee.mapReduce(
  function() { emit(this.firstName, this.salary); },
  function(key, values) {return Array.sum(values)}, {  
     query:{salary: {$lt : 5000}},
     out:"result"
  }
) .find()
Output>
{ "_id" : "ank", "value" : 3000 }
{ "_id" : "sag", "value" : 7000 }


4) But, how above query works? How mapReduce function works in above query?




STEP 1 : Query stage >


query:{salary: {$lt : 5000}}


With execution of this we find all the documents where salary < 5000.
( i.e. only 4 documents are left )


{ "_id" : 1, "firstName" : "ank", "salary" : 1000 }
{ "_id" : 2, "firstName" : "ank", "salary" : 2000 }
{ "_id" : 3, "firstName" : "sag", "salary" : 3000 }
{ "_id" : 4, "firstName" : "sag", "salary" : 4000 }

STEP 2 : Map stage >


function() { emit(this.firstName, this.salary); },


Map is formed which contains group of employee with same firstName and their individual salary in array.


{"ank" : [ 1000, 2000 ] }
{"sag" : [ 3000, 4000 ] }

STEP 3 : Reduce stage >
function(key, values) {return Array.sum(values)}, {  


Finally in stage reduce stage we can see final result, all the array elements are summed up.
{ "_id" : "ank", "value" : 3000 }
{ "_id" : "sag", "value" : 7000 }

5) Summary -
So in this MongoDB tutorial we learned Map Reduce function is used to process the huge volume of data.


EXAMPLE - To use mapReduce() function >
QUERY = Find sum of salary of all employee with same firstName where salary < 5000 in collection in MongoDB >
SOLUTION >
db.employee.mapReduce(
  function() { emit(this.firstName, this.salary); },
  function(key, values) {return Array.sum(values)}, {  
     query:{salary: {$lt : 5000}},
     out:"result"
  }
) .find()


How mapReduce function works in above query?


STEP 1 is Query stage >
query:{salary: {$lt : 5000}}


STEP 2 is Map stage >
function() { emit(this.firstName, this.salary); },


STEP 3 is Reduce stage >
function(key, values) {return Array.sum(values)}, {  


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>

What is MongoDB - A quick introduction to database

Labels: MongoDB
eEdit
Must read for you :