Created
May 13, 2012 11:42
-
-
Save umit/2687992 to your computer and use it in GitHub Desktop.
MongoDB Product MapReduce 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
map = function map() { | |
emit(this.type, {productIds : [this._id]}) | |
} | |
reduce = function reduce(key, values) { | |
var productIds = []; | |
var result = { productIds:[] }; | |
values.forEach(function(value) { | |
value.productIds.forEach(function(idValue){ | |
productIds.push(idValue); | |
}); | |
}); | |
result.productIds = productIds; | |
return result; | |
} | |
> db.product.mapReduce(map,reduce,{out:"productMapReduce4"}) | |
{ | |
"result" : "productMapReduce4", | |
"timeMillis" : 70, | |
"counts" : { | |
"input" : 6, | |
"emit" : 6, | |
"reduce" : 2, | |
"output" : 2 | |
}, | |
"ok" : 1, | |
} | |
> db.productMapReduce4.find().pretty() | |
{ | |
"_id" : "Shirt", | |
"value" : { | |
"productIds" : [ | |
ObjectId("4faec33dd57e4ace158b7e9f"), | |
ObjectId("4faec64bd57e4ace158b7ea0"), | |
ObjectId("4faec65ad57e4ace158b7ea1") | |
] | |
} | |
} | |
{ | |
"_id" : "Sweat Shirt", | |
"value" : { | |
"productIds" : [ | |
ObjectId("4faf8768d001a2b51cf047a0"), | |
ObjectId("4faf877ad001a2b51cf047a1"), | |
ObjectId("4faf878dd001a2b51cf047a2") | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment