Reputation: 773
I'm attempting to use the mapReduce function of Mongodb via Mongoose, but the map function I'm passing in is never called. Here is the data currently contained in the "Post" model collection:
[ { 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' ] } ]
Here is the code:
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);
});
The "model" and "stats" objects are always undefined, and the log statements in the map function are never called. If I do something like this with the Post model outside of the mapReduce function, I get the data at the top of the post as expected:
Post.find().exec(function(err, posts) {
console.log(posts);
});
Any suggestions? I'm sure something is just slightly off...
Upvotes: 1
Views: 1737
Reputation: 340
To debug your map/reduce/finalize functions you can use the MongoDB print statement. The output will be added to your Mongo log file.
Upvotes: 1
Reputation: 311835
You can't call console.log
from within the map
and reduce
functions as it's not supported by Mongo's JavaScript engine.
Upvotes: 5