Mongoose和mapReduce - 没有调用map函数

Geo*_*off 1 mongoose mongodb node.js

我试图通过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' ] } ]
Run Code Online (Sandbox Code Playgroud)

这是代码:

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);
});
Run Code Online (Sandbox Code Playgroud)

"model"和"stats"对象始终未定义,并且从不调用map函数中的日志语句.如果我使用mapReduce函数之外的Post模型执行类似的操作,我会按预期获取帖子顶部的数据:

Post.find().exec(function(err, posts) {
    console.log(posts);
});
Run Code Online (Sandbox Code Playgroud)

有什么建议?我确定有些东西稍微偏了......

Joh*_*yHK 5

你不能调用console.log从内部mapreduce,因为它不是由蒙戈的JavaScript引擎支持的功能.