Skip to content

Instantly share code, notes, and snippets.

@umit
Created May 13, 2012 11:26
Show Gist options
  • Save umit/2687931 to your computer and use it in GitHub Desktop.
Save umit/2687931 to your computer and use it in GitHub Desktop.
MongoDB Product MapReduce 3
map = function(){
var size;
for (size in this.sizes) {
emit(size, {size: this.sizes[size], price: this.sizes[size] * this.price });
}
};
reduce = function reduce(key,values){
var result = { sizeCount: 0, priceCount: 0 };
values.forEach(function(value){
result.sizeCount += value.size;
result.priceCount += value.price;
});
return result;
}
> db.product.mapReduce(map,reduce,{out:"productMapReduce3"})
{
"result" : "productMapReduce3",
"timeMillis" : 67,
"counts" : {
"input" : 6,
"emit" : 18,
"reduce" : 3,
"output" : 3
},
"ok" : 1,
}
> db.productMapReduce3.find()
{ "_id" : "L", "value" : { "sizeCount" : 11, "priceCount" : 215 } }
{ "_id" : "XL", "value" : { "sizeCount" : 19, "priceCount" : 450 } }
{ "_id" : "XXL", "value" : { "sizeCount" : 24, "priceCount" : 580 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment