Skip to content

Instantly share code, notes, and snippets.

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