MongoDB MapReduce:map函数实例中的全局变量?

Luc*_*lis 7 mapreduce mongodb

我在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';
  }
}
Run Code Online (Sandbox Code Playgroud)

这是不允许在MongoDB的MapReduce实现中,还是我在JavaScript中做错了(在JS中没有经验)?

Gat*_* VP 5

看一下这些文档,我发现了以下内容:

db.runCommand(
 { mapreduce : <collection>,
   map : <mapfunction>,
   reduce : <reducefunction>
   [, scope : <object where fields go into javascript global scope >]
 }
);
Run Code Online (Sandbox Code Playgroud)

我认为"范围"变量就是你所需要的.

在Github上有一个使用"范围"变量的测试/示例.

我对这些东西还不熟悉,但希望这足以让你开始.