I have the following MapReduce script that I'm working with:
splitAndGroupServices = function(groupMembers) {
var mapFunction = function() {
for(var idx in this.services) {
var service = this.services[idx];
if(service.member_id in groupMembers)
emit(service.member_id, service);
}
}
var reduceFunction = ...;
var finalizeFunction = ...;
db.items.mapReduce(mapFunction, reduceFunction, {out: {inline:1}, finalize: finalizeFunction});
}
However, when I execute:
db.loadServerScripts();
splitAndGroupServices({b1: 0, b2: 1});
I encounter the error message:
"errmsg" : "exception: ReferenceError: groupMembers is not defined near 'ber_id in members) { emit(ser' (line 4)",
I am struggling to figure out how to pass a variable from an outer function into an inner function. While this is feasible in JavaScript, MongoDB seems to be resistant to it. Any suggestions on how to work around this limitation?