i have a question about passing an arugment in MongoDB javascript to a map function.
Currently, what i had in mind is something like this:
var map = function(n) {
if(this.x == n){
emit(this.x);
}
}
var reduce = function(key, values) {
values.forEach(function(x) {
//do something
});
return {nd:values};
}
db.smsdb.mapReduce(map(2), reduce, "collection")
But as i have tried to do this, the shell returns an error “not code”…so i’m guessing i’m not doing this the right way.
Does anyone have the right solution for this kind of problem, i would be more than glad to get it right.
Thanks
This line:
is calling
map(2)with the result (undefined) being passed as the map function formapReduce.Instead do something like this:
UPDATE
The above doesn’t work because the
mapfunction isn’t available in the scope themapReducemap function is run. So you have to wrap it up into a single function that can generate the map function you need: