original collection:
{
"_id" : ObjectId('xxxxxxxxxxxxxxxx'),
"userId" : ObjectId('yyyyyyyyyyyyyyyy'),
"urlId" : ObjectId('zzzzzzzzzzzzzzzzzz')
},
{
"_id" : ObjectId('uuuuuuuuuuuuuuuuuu'),
"userId" : ObjectId('aaaaaaaaaaaaaaaa'),
"urlId" : ObjectId('zzzzzzzzzzzzzzzzzz')
}
emit:
emit(this.urlId, {userId: this.userId, visitCount: this.visitCount});
reduce:
function(key, values) {
var visitCount = 0;
var userVC = new Array();
values.forEach(function(doc) {
NOT SURE WHAT TO PUT HERE TO ACHIEVE DESIRED OUTPUT COLLECTION
});
return {urlId: key, userVC: userVC};
};
desired MR output collection:
{
"_id" : ObjectId('zzzzzzzzzzzzzzzzzzzz'),
"value" : {
"urlId" : ObjectId('zzzzzzzzzzzzzzzzzzzz'),
"userVC" : {
ObjectId('yyyyyyyyyyyyyyyy') : <total visit count for this userId on this urlId>,
ObjectId('aaaaaaaaaaaaaaaa') : <total visit count for this userId on this urlId>
}
}
You want to know how many times each user visited each individual url on your site? I think you will want to solve this differently.
Emit a count of url/user visits:
Count them with the reduce:
Then if you really really wanted the desired output you stated, you could do that in a finalize step. But I think it doesn’t scale well to N users.
Here is a link that doesn’t exactly satisfy your stated goal, but I found very useful when trying to understand how these mongodb mapreduce functions work:
http://cookbook.mongodb.org/patterns/unique_items_map_reduce/