3

我有一个集合,可以说以下内容:

{
    "_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 或聚合,以提供以下输出(示意性地):

user_id, category, "点赞数"

不知何故,我找不到合适的解决方案......到目前为止,我得到的是喜欢的类别的总聚合,但不是每个 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
});

有人可以给我一个提示吗?非常感谢您的帮助!

托比

4

2 回答 2

2

如果您想使用 MR 来计算每个用户的点赞数,您可以发出 user_id 和 category 作为用于在 reduce 中对文档进行分组的键:

map: function() {
    var u = this.user_id; 
    this.likes.data.forEach(
       function(z){
            emit( {category: z.category, user: u} , { count : 1 } );
        }
    );
}
于 2012-09-14T22:29:56.310 回答
1

If possible, I would recommend using the new aggregation framework, which comes with MongoDB version 2.2, the newest stable release. The aggregation framework is written in C++ rather than Javascript, and should have better performance for many aggregation commands.

The following aggregate() counts the number of likes per category, per user. Please let me know if this is not the desired output.

Command:

    db.collection.aggregate(
        { $unwind : "$likes.data" }, 
        { $group : 
           {
             _id: {user: "$user_id", category: "$likes.data.category"}, 
             count: {$sum:1}
           }
        }
   );

Result:

{
    "result" : [
        {
            "_id" : {
                "user" : "978956784678",
                "category" : "Retail and consumer merchandise"
            },
            "count" : 2
        }
    ],
    "ok" : 1
}
于 2012-09-14T21:44:44.743 回答