我正在尝试通过Mongoose使用Mongodb的mapReduce函数,但是从未调用我传递的map函数。以下是“发布”模型集合中当前包含的数据:
[ { 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);
});
“模型”和“状态”对象始终是未定义的,并且永远不会调用map函数中的log语句。如果我在mapReduce函数之外对Post模型执行类似的操作,则可以按预期将数据放在帖子顶部:
Post.find().exec(function(err, posts) {
console.log(posts);
});
有什么建议么?我敢肯定有些事要解决了...
最佳答案
您不能在console.log
和map
函数中调用reduce
,因为Mongo的JavaScript引擎不支持它。
关于node.js - Mongoose 和mapReduce-未调用 map 函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15167639/