Skip to content

MongoDB Aggregation – MapReduce

    1.	Define the Map Function 
    >	var mapFunction1 = function () { emit(this.store,this.available);}; 
    
    2.	Define the Reduce Function 
    >	var reduceFunction1 = function(keyStore,valuesAvailable){return Array.sum(valuesAvailable);}; 
    
    3.	Perform the map-reduce 
    >db.mybookstore.mapReduce(mapFunction1,reduceFunction1,{out:"map_reduc e_example1"})
    {
    "result" : "map_reduce_example1", "timeMillis" : 131,
    "counts" : { "input" : 12, "emit" : 12, "reduce" : 2, "output" : 5
    },
    "ok" : 1,
    }
    
    4.	Check the Output collection 
    >	db.map_reduce_example1.find().pretty() 
    { "_id" : "cine book office", "value" : 17 } { "_id" : "laxman book depot", "value" : 32 } { "_id" : "raman book gallery", "value" : 22 } { "_id" : "royal book stores", "value" : 258 } { "_id" : "sameer bandhu", "value" : 78 } 

    Also See:

    MongoDB Aggregation – Pipeline

    Map-Reduce to Aggregation Pipeline

    Leave a Reply