Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

mongooseでMapReduceを使う

More than 5 years have passed since last update.

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

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

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);
});
yasuno45
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away