1

Mongooseを介してMongodbのmapReduce関数を使用しようとしていますが、渡したmap関数が呼び出されません。現在「Post」モデルコレクションに含まれているデータは次のとおりです。

[ { data: 'Tag test data',
name: 'Tag Test',
_id: 5130dff2560105c235000002,
__v: 0,
comments: [],
tags: [ 'tag1', 'tag2', 'tag3' ] },


 { data: 'Testing tags.  Again.',
    name: 'Another test post',
    _id: 5131213b611fe1f443000002,
    __v: 0,
    comments: [],
    tags: [ 'tags', 'test', 'again' ] } ]

コードは次のとおりです。

var Schema = mongoose.Schema;
var PostSchema = new Schema ({
   name : String
   , data : String
   , tags : [String]
});
mongoose.model('Post', PostSchema);


var o = {};

o.map = function() {
    if (!this.tags) {
        //console.log('No tags found for Post ' + this.name);
        return;
    }
    for (index in this.tags) {
        emit(this.tags[index], 1);
    }
}

o.reduce = function(previous, current) {
    var count = 0;
    for (index in current) {
        count += current[index];
    }
    return count;
}

o.out = { replace : 'tags'}
o.verbose = true;

var Post = mongoose.model('Post');

Post.mapReduce(o, function(error, model, stats) {
    console.log('model: ' + model);
    console.log('stats: ' + stats);
});

「model」オブジェクトと「stats」オブジェクトは常に未定義であり、map関数のlogステートメントが呼び出されることはありません。mapReduce関数の外部でPostモデルを使用してこのようなことを行うと、期待どおりに投稿の上部にデータが表示されます。

Post.find().exec(function(err, posts) {
    console.log(posts);
});

助言がありますか?何かが少しずれていると確信しています...

4

2 に答える 2

5

Mongo の JavaScript エンジンではサポートされていないため、 and関数console.log内から呼び出すことはできません。mapreduce

于 2013-03-03T14:10:50.447 に答える