Mongodb Node-mongodb-native Map/reduce
I have a map/reduce method using node-mongodb-native. I'm trying to only return distinct records on 'product_id'. This is what I have so far: var map = function() {
Solution 1:
In fact, an example of what your trying to do:
var map = function() {
emit(this.product_id, {"_id" : this._id, "name" : this.name });
}
var finailise = function(key, value){
return value[0]; // This might need tweaking, ain't done MRs in a while :/
}
Do note however there are two types of distinct:
- First find
- Last find
There is no standard way to distinct and each DB has it's own methods, it isn't even standard across SQL databases so it is upto you to know which way you want to distinct. The one above does a first find distinct. You can do a last find distinct like:
var finailise = function(key, value){
return value[value.length-1];
}
Or something similar, should get you started anyway.
Hope it helps,
Post a Comment for "Mongodb Node-mongodb-native Map/reduce"