我在MongoDB中编写了一个MapReduce,并希望使用全局变量作为写入/读取的缓存。我知道在地图函数实例中不可能有全局变量 - 我只想在每个函数实例中使用全局变量。这种类型的功能存在于Hadoop的MapReduce中,所以我期待它在MongoDB中存在。但以下似乎不起作用:
var cache = {}; // Does not seem to work!
function () {
var hashValue = this.varValue1 + this.varValue2;
if(typeof(cache[hashValue])!= 'undefined') {
// Do nothing, we've processed at least one input record with this hash
} else {
// Process the input record
// Cache the record
cache[hashValue] = '1';
}
}
这是不允许在MongoDB的MapReduce实现中,还是我在JavaScript中做错了(在JS中没有经验)?
答案 0 :(得分:5)
查看docs,我发现以下内容:
db.runCommand(
{ mapreduce : <collection>,
map : <mapfunction>,
reduce : <reducefunction>
[, scope : <object where fields go into javascript global scope >]
}
);
我认为“范围”变量是您所需要的。
有一个测试/示例on Github使用“范围”变量。
我还是新手,但希望这足以让你开始。
答案 1 :(得分:1)
正如盖茨副总裁所说,你需要在全球范围内添加缓存。因此,要提供完整的答案,请考虑您的脚本,这是您需要做的:
db.runCommand(
{ mapreduce : <your collection>,
map : <your map function, or reference to it>,
reduce : <your reduce function, or reference to it>,
scope : { cache : {} }
}
);
该命令会将“scope”对象参数的内容注入到全局上下文中。然后,缓存将根据您在地图功能中的使用方式进行操作。我已经测试了这个。