如何在NodeJS中使用MongoDB中的mapReducevariables

我有一个看起来像一个事件的集合

{ _id: BSONID name: "event_name", values: {a: 10, b: 1000, c: 50} } 

我试图使用mapreduce他们使用

 map = function() { return emit([this.name, this.values['a']], this.values['b']); } reduce = function(key, values) { // stuff } collection.mapReduce(map, reduce, { out: { inline: 1 } }, callback); 

不过,我希望能够dynamic地改变我映射哪些值。 本质上,我想有

 var key = 'a'; var value = 'b'; map = function () { return emit([this.name, this.values[key]], this.values[value]); } 

问题是执行上下文不会传递给mongodb。 任何不依赖使用string函数的解决scheme?

是的,您可以将“范围”variables传递给MapReduce:

 scope = {key : "a", value : "b"}; collection.mapReduce(map, reduce, {scope : scope, out: { inline: 1 } }, callback);