我有一个集合,假设它的内容如下:
{
"_id":ObjectId("5051c4778ec2487f7c000001"),
"user_id":"978956784678",
"likes":{
"data":[
{
"name":"Store 1",
"category":"Retail and consumer merchandise",
"id":"354412263434",
"created_time":"2012-09-07T11:36:05+0000"
},
{
"name":"Store 2",
"category":"Retail and consumer merchandise",
"id":"293088074081904",
"created_time":"2012-08-13T20:06:49+0000"
}
],
"paging":{
"next":"https://test.com/next"
}
}
}
我正在尝试在MongoDB中构建Map/Reduce或聚合,以给出以下输出(原理图):
用户ID,类别,“点赞数”
我还没有找到合适的解决方案......目前我得到的是每个类别点赞总数的聚合,而不是按user_id的聚合。
db.runCommand({
mapreduce: "likes",
map: function() {
this.likes.data.forEach(
function(z){
emit( z.category , { count : 1 } );
}
);
},
reduce: function(key, values) {
var total = 0;
for ( var i=0; i<values.length; i++ )
total += values[i].count;
return { count : total };
},
out: 'result3',
verbose: true
});
有人能给我一个提示吗?非常感谢帮助! 托比
emit( this.user_id , { count : 1 } );
怎么样? - Sreenath S