How to count occurence of each value in array?

对着背影说爱祢 提交于 2020-01-02 15:51:36

问题


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?

I've tried

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

but no luck :(

A Sample:

{
        "_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"
                }
        ]
}

回答1:


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},
});

UPDATE

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
}



回答2:


Just a snide answer here to compliment @JohnnyHKs answer: it sounds like your new to MongoDB and as such possibly working on a new version of MongoDB if that is the case (if not I would upgrade) either way the old group count is kinda bad. It won't, for one, work with sharding.

Instead in MongoDB 2.2 you can just do:

db.col.aggregate({$group: {_id: "$comments.username", count: {$sum: 1}}})

Or something similar. You can read more about it here: http://docs.mongodb.org/manual/applications/aggregation/



来源:https://stackoverflow.com/questions/14319616/how-to-count-occurence-of-each-value-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!