2
2

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でmap-reduceしている時に嵌ったこと。

Posted at

例えば以下のような雰囲気で書いた。

        var command = {
            mapreduce:"communications",
            query:query,
            map:communicationCountMapFunc,
            reduce:communicationCountReduceFunc,
            out:'communications_mr_buf'
        };

        var dbh = new DPH(Promise); //これはPromiseをDB対応させるラッパ
        mongoose.connection.db.executeDbCommand(command, dbh.handler(function (ret) {
            return ret.documents[0];
        }));
        return dbh.promise;

後でcommunications_mr_bufテーブルから結果を引っ張って来ることになる。下記のように(ここをクールに書く方法は知りたい。モデルとして定義しないでも良い処理なので)。

                var dbh = new DPH(Promise);
                var CommunicationBufEntity = mongoose.model('communications_mr_buf', CommunicationBufSchema);//スキーマは別途定義しておいて。

                CommunicationBufEntity.find(query, {}, options, dbh.handler());
                return dbh.promise;

ただ、Mongooseのモデルの基本的なルールが、mongoose.modelの第一引数のオブジェクト名には's'が自動的に付与される動きだったので、communications_mr_bufsを検索してしまい、上記のままでは一件も引っかからない。

と言うわけで最初に出力するテーブルに's'を付けて回避した。おかげで数時間無駄にしてしまったのであった。クールな方法があったら知りたいです。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?