Reputation: 34725
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
?
Upvotes: 0
Views: 993
Reputation: 994
After an hour of failed attempts...!
let maps = function() {
emit(this. user_id, 1)
};
let reduces = function(keys, vals){
return Array.sum(vals);
};
result.collection.mapReduce(maps, reduces, { out: { reduce: 'test_collection'
}, jsMode: true, verbose: true})
.then(res=> console.log(res.stats))
.catch(err=> console.log({err}));
..I established that 'mapReduce' does not accept 'Fat Arrows' =>=>=>=>|_ Maps || Reduces!!
Upvotes: 0
Reputation: 36764
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 } } } );
Upvotes: 2