Reputation: 7981
I have the following script in MapReduce:
splitAndGroupServices = function(members) {
var mapFn = function() {
for(var j in this.services) {
var service = this.services[j];
if(service.member_id in members)
emit(service.member_id, service);
}
}
var reduceFn = ...;
var finalizeFn = ...;
db.items.mapReduce(mapFn, reduceFn, {out: {inline:1}, finalize: finalizeFn});
}
When I call:
db.loadServerScripts();
splitAndGroupServices({b1: 0, b2: 1});
I keep getting error:
"errmsg" : "exception: ReferenceError: members is not defined near 'ber_id in members) { emit(ser' (line 4)",
How can I pass the outer function variable into an inner function variable? In JS it is actually possible but Mongo doesn't like it.
Upvotes: 0
Views: 35
Reputation: 50416
The mapReduce command supports a "scope" which is shared across mapper
, reducer
and finalize
stages:
db.items.mapReduce(
mapFn,
reduceFn,
{
"scope": { "members": members },
"out": { "inline": 1 },
"finalize": finalizeFn
}
)
This is treated as a "global" for the process and any modifications are carried into the next stage or similarly available in that stage. It is a standard way of "passing variables" for use in any of the stage functions.
Upvotes: 1