Subscribe via RSS Feed

Map Reduce Programming in MongoDB Shell

June 9, 2013 1 Comment

Here we will give a simple example of executing Map Reduce Job in MongoDB Shell.

For beginners we have a quick start-up guide in MongoDB.

We expect – readers of this post have knowledge in

1> Map Reduce Framework

2> MongoDB Primary Concepts

In MongoDB, different scenarios can be represented as Map Reduce Job.

Our Problem Scenario is – to find the average price for Seed within a given date-range.

The Data set for the problem is attached here. Reader can try with this.

So, now the coding -

A> Map Function -

 // We are counting the seed price with counter in the dataset. 
 // The emitting output for the Map function, will be like map(<<seedname as key>>,
                                                                <<price of the seed with counter>>)

var mapFunction2 = function() {
      var doc = this.seedprice;
      var key = this.seedname;
      var price =0;	
      var count =0;
      doc.forEach(function(seed) {
	price += seed.price;
	count +=1;
        var value = {
            count: 1,
            price: seed.price
        };
	emit(key, value);
  });
};

B> Reduce Function -

 //Reduce funcion will sum up the prices for seed.
var reduceFunc2 = function(key, countObjVals) {
                     reducedVal = { count: 0, price: 0 };
                     for (var idx = 0; idx < countObjVals.length; idx++) {
                         reducedVal.count += countObjVals[idx].count;
                         reducedVal.price += countObjVals[idx].price;
                     }
                     return reducedVal;
                  };

C> Finalize Function -

 // Calculating average price for the seed.
var finalizeFunc2 = function (key, reducedVal) {
       reducedVal.avg = reducedVal.price/reducedVal.count;
       return reducedVal;
};

D> Executing Map Reduce in MongoDB by combining above functions -

 // The final mapreduce function
var avg = db.seeds.mapReduce( mapFunction2 , reduceFunc2,
       {
         out: { merge: "map_reduce_example" },
         finalize: finalizeFunc2
       }
    )

E> To see the result, we should execute -

 
db[avg.result].find();

For the above given dataset the result will look something like -

 
{ "_id" : "Amidon", "value" : { "count" : 56, "price" : 12230, "avg" : 218.39285714285714 } }
{ "_id" : "Arachide", "value" : { "count" : 56, "price" : 29195, "avg" : 521.3392857142857 } }
{ "_id" : "Bêtê Bêtê", "value" : { "count" : 56, "price" : 4170, "avg" : 74.46428571428571 } }
{ "_id" : "Florido", "value" : { "count" : 56, "price" : 3355, "avg" : 59.910714285714285 } }
{ "_id" : "Krenglê", "value" : { "count" : 56, "price" : 9830, "avg" : 175.53571428571428 } }
{ "_id" : "Piment court", "value" : { "count" : 56, "price" : 80785, "avg" : 1442.5892857142858 } }
{ "_id" : "Piment long", "value" : { "count" : 56, "price" : 56610, "avg" : 1010.8928571428571 } }
{ "_id" : "Pistache", "value" : { "count" : 56, "price" : 46120, "avg" : 823.5714285714286 } }
{ "_id" : "Poivron", "value" : { "count" : 56, "price" : 21700, "avg" : 387.5 } }
{ "_id" : "Soja", "value" : { "count" : 56, "price" : 8075, "avg" : 144.19642857142858 } }

where avg is our desired average price for given seeds.

Reference of the Post -

http://docs.mongodb.org/manual/tutorial/map-reduce-examples/

Enter your email address:

Delivered by FeedBurner