如何计算数组中每个值的出现次数? [英] How to count occurence of each value in array?

查看:17
本文介绍了如何计算数组中每个值的出现次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB中有一个ISSUES的数据库,有些issue有注释,是一个数组;每个评论都有一个作者.如何计算每个作者发表的评论数?

I have a database of ISSUES in MongoDB, some of the issues have comments, which is an array; each comments has a writer. How can I count the number of comments each writer has written?

我试过了

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
Excerpting 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"
                }
        ]
}

推荐答案

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

You forgot the curly braces on the key value and you need to terminate that line with a , instead of a ;.

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

更新

在意识到 comments 是一个数组之后...您需要为此使用 aggregate 以便展开"comments然后分组:

After realizing comments is an array...you'd need to use aggregate for that so that you can 'unwind' comments and then group on it:

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

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

For the sample doc in the question, this outputs:

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

这篇关于如何计算数组中每个值的出现次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆