Skip to content

Instantly share code, notes, and snippets.

@umit
Created May 13, 2012 10:19
Show Gist options
  • Save umit/2687602 to your computer and use it in GitHub Desktop.
Save umit/2687602 to your computer and use it in GitHub Desktop.
MongoDB Product MapReduce 1
map = function map(){
//key olarak product type
//value olarak ise obje icinde product price alanına gore mapliyoruz
emit(this.type, {price:this.price} );
}
reduce = function reduce(key,values) {
var result = { sumProductPrice: 0 };
values.forEach(function(value) {
result.sumProductPrice += value.price
});
return result;
}
> db.product.mapReduce(map, reduce, {out:"productMapReduce1"})
{
"result" : "productMapReduce1",
"timeMillis" : 377,
"counts" : {
"input" : 6,
"emit" : 6,
"reduce" : 2,
"output" : 2
},
"ok" : 1,
}
> db.productMapReduce1.find()
{ "_id" : "Shirt", "value" : { "sumProductPrice" : 50 } }
{ "_id" : "Sweat Shirt", "value" : { "sumProductPrice" : 95 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment