如何计算数组中每个值的出现次数?

时间:2013-01-14 13:47:16

标签: arrays mongodb mapreduce

我在MongoDB中有一个ISSUES数据库,其中一些问题有注释,这是一个数组;每个评论都有一个作家。我如何计算每位作者写的评论数量?

我试过

db.test.issues.group(
{
    key = "comments.username":true;
    initial: {sum:0},
    reduce: function(doc, prev) {prev.sum +=1},
    }
);

但没有运气:(

样本:

{
        "_id" : ObjectId("50f48c179b04562c3ce2ce73"),
        "project" : "Ruby Driver",
        "key" : "RUBY-505",
        "title" : "GETMORE is sent to wrong server if an intervening query unpins the connection",
        "description" : "I've opened a pull request with a failing test case demonstrating the bug here: https://github.com/mongodb/mongo-ruby-driver/pull/134\nExcerpting that commit message, the issue is: If we do a secondary read that is large enough to require sending a GETMORE, and then do another query before the GETMORE, the secondary connection gets unpinned, and the GETMORE gets sent to the wrong server, resulting in CURSOR_NOT_FOUND, even though the cursor still exis ts on the server that was initially queried.",
        "status" : "Open",
        "components" : [
                "Replica Set"
        ],
        "affected_versions" : [
                "1.7.0"
        ],
        "type" : "Bug",
        "reporter" : "Nelson Elhage",
        "priority" : "major",
        "assignee" : "Tyler Brock",
        "resolution" : "Unresolved",
        "reported_on" : ISODate("2012-11-17T20:30:00Z"),
        "votes" : 3,
        "comments" : [
                {
                        "username" : "Nelson Elhage",
                        "date" : ISODate("2012-11-17T20:30:00Z"),
                        "body" : "Thinking some more"
                },
                {
                        "username" : "Brandon Black",
                        "date" : ISODate("2012-11-18T20:30:00Z"),
                        "body" : "Adding some findings of mine to this ticket."
                },
                {
                        "username" : "Nelson Elhage",
                        "date" : ISODate("2012-11-18T20:30:00Z"),
                        "body" : "I think I tracked down the 1.9 dependency."
                },
                {
                        "username" : "Nelson Elhage",
                        "date" : ISODate("2012-11-18T20:30:00Z"),
                        "body" : "Forgot to include a link"
                }
        ]
}

2 个答案:

答案 0 :(得分:3)

您忘记了key值上的花括号,您需要使用,而不是;来终止该行。

db.issues.group({
    key: {"comments.username":true},
    initial: {sum:0},
    reduce: function(doc, prev) {prev.sum +=1},
});

<强>更新

在认识到comments是一个数组之后......您需要使用aggregate,以便您可以“展开”comments然后对其进行分组:

db.issues.aggregate(
    {$unwind: '$comments'},
    {$group: {_id: '$comments.username', sum: {$sum: 1}}}
);

对于问题中的示例文档,此输出:

{
  "result": [
    {
      "_id": "Brandon Black",
      "sum": 1
    },
    {
      "_id": "Nelson Elhage",
      "sum": 3
    }
  ],
  "ok": 1
}

答案 1 :(得分:1)

这里只是一个讽刺的回答来赞美@JohnnyHKs回答:这听起来像是MongoDB的新手,因此可能会在新版本的MongoDB上工作,如果是这样的话(如果不是我会升级的话)那么旧的{{ 1}}计数有点不好。例如,它不会与分片一起使用。

而在MongoDB 2.2中你可以这样做:

group

或类似的东西。您可以在此处详细了解:http://docs.mongodb.org/manual/applications/aggregation/