2013-01-14 179 views
2

我有一個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

你忘了在key值大括號,你需要終止該行用,代替;

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

UPDATE

後實現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 
} 
+0

仍然有錯誤:SyntaxError:missing:在屬性id(shell)之後:2 – Ace

+0

@Ace用':'替換''''後面的'key'。查看更新的答案。 – JohnnyHK

+0

謝謝約翰尼!這解決了問題,但結果仍然不好! 它無法識別「comments.username」,結果如下: [{「comments.username」:null,「sum」:12}] 其中12是記錄總數! – Ace

1

只是一個諷刺答案在這裏讚美@JohnnyHKs答案:這聽起來像你的新的MongoDB,因此可能在新版本的MongoDB上工作,如果是這樣的話(如果不是我會升級)舊group計數是有點不好。就其中一個而言,它不會與分片一起工作。

相反MongoDB中2.2,你可以這樣做:

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

或類似的東西。你可以在這裏閱讀更多關於它的信息:http://docs.mongodb.org/manual/applications/aggregation/

+0

我已經看過這個文檔,但我無法完成這個任務! – Ace

相關問題