Map Reduce in Mongo db :

This Blog will teach you, how to write Map reduce in Mongo DB .

Map Reduce is a concept that process large volume of data into aggregated results.

To use Map Reduce Concept in Mongo DB , create one command called “mapReduce”.

This mapReduce() function fetch data from collection (table) and then produce the result set into (key, value) format.

Then reduce () function takes the (key, value) pair and reduce all the data (documents) on the same key.

Eg : – Let say I have two Collection (tables) named :

  1. Emp_test
  2. Dept_Test

Now , to create collection in mongo db , use below query

db.createCollection(“Emp_test”)

db.createCollection(“Dept_Test”)

To insert data in Emp_test Collection :

db.Emp_test.insert({“name” : {       “first” : “ABC”,       “last” : “DEF”   },   “city” : “Hyd”,   “department” : 1})

db.Emp_test.insert({“name” : {       “first” : “GHI”,       “last” : “JKL”   },   “city” : “Pune”,   “department” : 2})

To insert data in Dept_Test Collection :

db.Dept_Test.insert({“_id” : 1,   “department” : “SALESMAN”})

db.Dept_Test.insert({“_id” : 2,   “department” : “CLERK”})

Now the requirement is to display FirstName , LastName , DepartmentName.

For this , we need to use Map Reduce :

# 1 : Create two map functions for both the collections.

var mapEmp_test = function () {

var output= {departmentid : this.department,firstname:this.name.first, lastname:this.name.last , department:null}

emit(this.department, output);               };

var mapDept_Test = function () {

var output= {departmentid : this._id,firstname:null, lastname:null , department:this.department}

emit(this._id, output);               };

Write Reduce Logic to display the required fields :

var reduceF = function(key, values) {

var outs = {firstname:null, lastname:null , department:null};

values.forEach(function(v){

if(outs.firstname ==null){                       outs.firstname = v.firstname                   }                   if(outs.lastname ==null){                       outs.lastname = v.lastname                   }                   if(outs.department ==null){                       outs.department = v.department                   }                          });   return outs;};

# 3 : Store the result into a different collection called emp_dept_test

result = db.employee_test.mapReduce(mapEmployee, reduceF, {out: {reduce: ’emp_dept_test’}}) result = db.department_test.mapReduce(mapDepartment,reduceF, {out: {reduce: ’emp_dept_test’}})

# 4: write the following commanddb.emp_dept_test.find()

1 comment

  1. Magesh

    Hi, Could i know how insert, update key:value pairs by passing parameters using map reduce function in mongo db. i was deployed in a mongo db project and i can able to pass the static values but i need to know how to pass the parameters dynamically. please help me in this regard.

Leave a Reply