私はmongodbmapreduce用のnode.jsルーターを持っています:
app.get('/api/facets/:collection/:groupby', function(req, res) {
var collection = db.collection(req.params.collection);
var groupby = req.params.groupby;
var map = function() {
if (!this.region) {
return;
}
for (index in this.region) {
emit(this.region[index], 1);
}
}
var reduce = function(previous, current) {
var count = 0;
for (index in current) {
count += current[index];
}
return count;
}
var options = {out: groupby + '_facets'};
collection.mapReduce(map, reduce, options, function (err, collection) {
collection.find(function (err, cursor) {
cursor.toArray(function (err, results) {
res.send(results);
});
})
})
});
これはうまくいきます。groupby
しかし、私は自分のパラメータを使用したいと思います。私がこのようなことをしようとすると:
var map = function() {
if (!this[groupby]) {
return;
}
for (index in this[groupby]) {
emit(this[groupby][index], 1);
}
}
受け取りTypeError: Cannot call method 'find' of undefined
ます。そのような動的mapreduce関数を作成する方法はありますか?
ありがとう。
編集:
わお!自分でやる。scope
このようにparamをmapreduce引数に渡すだけで、 map関数内で実行し、代わりに変数を使用するscope:{keys: groupby}
ことができました。素晴らしい!var key = this[keys]
key
this.region