4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mongooseでMapReduceを使う

Posted at

色々調べてみると下記の方法で良いみたいです。

参考にさせて頂いたサイト様

var map = function() {
    if (!this.tags || this.contributor != '__USERID__') { return; }
    for (index in this.tags) {
        emit(this.tags[index], 1);
    }
};
var reduce = function(previous, current) {
    var count = 0;
    for (index in current) {
        count += current[index];
    }
    return count;
};

map = map.toString().replace('__USERID__', userId); // mapに変数を渡す方法がわからないので、文字列に強引に埋め込んでいます。イケテナイ。
reduce = reduce.toString();

db.model('Item').collection.mapReduce(map, reduce, {out: {inline: 1}}, function(err, val) {
    console.log(err);
    console.log(val);
});
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?