3

私は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"
                }
        ]
}
4

2 に答える 2

5

値の中括弧を忘れたため、その行を。の代わりに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
}
于 2013-01-14T14:05:10.573 に答える
1

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/

于 2013-01-14T14:09:31.513 に答える