Sample doc:
{
"_id" : ObjectId("51cd7274267d959cb9f95cea"),
"creation_time" : 100,
"delivered" : true,
"id" : 1,
"user_id" : 10
}
Map function:
db.system.js.save({
_id: "mapDummy",
value: function(){
emit(this.user_id,this.user_id);
}
});
Reduce function:
db.system.js.save({
_id: "reduceDummy",
value: function(key,values){
return Array.sum(values);
}
});
mapReduce(...) function call:
db.newsdb.mapReduce("mapDummy", "reduceDummy", {out: "notifications_result", query: {delivered:true}});
{
"result" : "notifications_result",
"timeMillis" : 16,
"counts" : {
"input" : 12,
"emit" : 0,
"reduce" : 0,
"output" : 0
},
"ok" : 1,
}
Why emit:0
?
I think this is because you use a string and not a function to the call:
db.newsdb.mapReduce("mapDummy", "reduceDummy", {out: "notifications_result", query: {delivered:true}});
Instead of Map/Reduce I would suggest you use the aggregation framework (A/F) anyway. You seem to be doing a simple group by on user_id, which is much better served with A/F as it's a lot faster and easier to use:
db.newsdb.aggregate( { $group: { _id: user_id, count: { $sum: 1 } } } );