Skip to content

Instantly share code, notes, and snippets.

@umit
Created May 13, 2012 12:24
Show Gist options
  • Save umit/2688182 to your computer and use it in GitHub Desktop.
Save umit/2688182 to your computer and use it in GitHub Desktop.
MongoDB Product MapReduce 5
map = function map() {
emit(this.type, {productColors :[this.color]});
}
reduce = function reduce(key, values) {
var colors = [];
var results = { colors:[] };
values.forEach(function(value){
value.productColors.forEach(function(colorValue){
colors.push(colorValue);
});
});
results.colors = colors;
return results;
}
> db.product.mapReduce(map,reduce,{out:"productMapReduce5"})
{
"result" : "productMapReduce5",
"timeMillis" : 6,
"counts" : {
"input" : 6,
"emit" : 6,
"reduce" : 2,
"output" : 2
},
"ok" : 1,
}
> db.productMapReduce5.find().pretty()
{
"_id" : "Shirt",
"value" : {
"colors" : [
"Green",
"Red",
"Yellow"
]
}
}
{
"_id" : "Sweat Shirt",
"value" : {
"colors" : [
"Black",
"Blue",
"Gray"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment