I'm trying to use an external object inside mongodb map/reduce functions. If the object has a variable which it should access, an error occurs.
For example:
var conn = new Mongo();
var db = conn.getDB("test");
var HelperClass = function() {
var v = [1, 2, 3];
this.data = function() {
return v;
};
};
var helper = new HelperClass();
var map = function() {
helper.data().forEach(function(value) {
emit(value, 1);
});
};
var reduce = function(key, values) {
var count = 0;
values.forEach(function(entry) {
count += entry;
});
return count;
};
db.test.mapReduce(map, reduce, {
out: "temp",
scope: {
helper: helper
}
});
The output from mongodb:
map reduce failed:{ "errmsg" : "exception: ReferenceError: v is not defined", "code" : 16722, "ok" : 0 } at src/mongo/shell/collection.js:970
Is it an expected behavior? Is there any other way to use external objects in mapReduce?