【发布时间】:2014-02-26 16:15:36
【问题描述】:
我正在尝试在 mongodb map/reduce 函数中使用外部对象。如果对象有一个它应该访问的变量,则会发生错误。
例如:
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
}
});
mongodb 的输出:
map reduce 失败:{ "errmsg" : "exception: ReferenceError: v is not 定义”,“代码”:16722,“确定”:0 } 在 src/mongo/shell/collection.js:970
这是预期的行为吗?有没有其他方法可以在 mapReduce 中使用外部对象?
【问题讨论】:
标签: javascript mongodb scope mapreduce