2016-09-27 39 views
0

在我收藏的聚合,每個文件代表一個用戶生成的測驗,幷包括標籤陣列領域,即歷史,科學,數學,等我想獲得一個與每個標籤關聯的文檔數量。獲取文檔的數量在根據具體需求

以下聚集結果在一個獨特的標籤列表看起來像這樣:{tags:["History", "Science", "Math"]}

db.quizzes.aggregate([  
      {$unwind: "$tags"},  
      {$group: {_id:null, tgs: {$addToSet: "$tags"}}},  
      {$project: {_id:0, tags: "$tgs"}},  
     ]) 

但是,可以在上述聚合還可以獲得包含每個標籤的文檔數量的計數?例如,如果有3次歷史測驗,2次科學測驗和5次數學測驗,結果將如下所示:{tags:[{tag: "History", count: 3}, {tag: "Science", count: 2}, {tag: "Math", count:5}]}

在此先感謝您提供任何提示。

編輯,包括收集資料:

{ 
    "_id" : ObjectId("57d8ccd573099cb013b462b5"), 
    "title" : "Presidential Trivia", 
    "quiz" : "[{\"question\":\"How many presidents were members of the Whig party?\",\"choices\":[\"Two\",\"Three\",\"Four\"],\"correct\":\"2\"},{\"question\":\"Who was the first president to be impeached?\",\"choices\":[\"Warren Harding\",\"Andrew Johnson\",\"Andrew Jackson\"],\"correct\":\"1\"},{\"question\":\"How many presidents died during their presidency?\",\"choices\":[\"Four\",\"Six\",\"Eight\"],\"correct\":\"2\"},{\"question\":\"How many presidents had no party affiliation?\",\"choices\":[\"One\",\"Two\",\"Three\"],\"correct\":\"0\"},{\"question\":\"Who was the only president to serve two non-consecutive terms, making him both the 22nd and 24th president?\",\"choices\":[\"John Quincy Adams\",\"Grover Cleveland\",\"Theodore Roosevelt\"],\"correct\":\"1\"}]", 
    "correctArray" : "[\"2\",\"1\",\"2\",\"0\",\"1\"]", 
    "author" : "jake2", 
    "createTime" : ISODate("2016-09-14T04:06:45.118Z"), 
    "likes" : 0, 
    "avgScore" : 0, 
    "plays" : 3, 
    "private" : "0", 
    "tags" : [ 
     "US Presidents", 
     "American History", 
     "History" 
    ] 
} 
{ 
    "_id" : ObjectId("57d8d08973099cb013b462b6"), 
    "title" : "Finance Quiz", 
    "quiz" : "[{\"question\":\"Which of these involves the analysis of of a business's financial statements, often used in stock valuation?\",\"choices\":[\"Fundamental Analysis\",\"Technical Analysis\",\"P/E ratio\"],\"correct\":\"0\"},{\"question\":\"What was the name of the bond purchasing program started by the U.S. Federal Reserve in response to the 2008 financial crisis?\",\"choices\":[\"Stimulus Package\",\"Quantitative Easing\",\"Mercantilism\"],\"correct\":\"1\"},{\"question\":\"Which term describes a debt security issued by a government, company, or other entity?\",\"choices\":[\"Bond\",\"Stock\",\"Mutual fund\"],\"correct\":\"0\"},{\"question\":\"Which of these companies has the largest market capitalization (as of October 2015)?\",\"choices\":[\"Ford Motors\",\"Apple\",\"Bank of America\"],\"correct\":\"1\"},{\"question\":\"Which of these is a measure of the size of an economy?\",\"choices\":[\"Purchasing Power Index\",\"Unemployment Rate\",\"Gross Domestic Product\"],\"correct\":\"2\"}]", 
    "correctArray" : "[\"0\",\"1\",\"0\",\"1\",\"2\"]", 
    "author" : "jake2", 
    "createTime" : ISODate("2016-09-14T04:22:33.756Z"), 
    "tags" : [ 
     "Finance" 
    ], 
    "likes" : 0, 
    "avgScore" : 0, 
    "plays" : 10, 
    "private" : "0" 
} 
{ 
    "_id" : ObjectId("57d8d24073099cb013b462b8"), 
    "title" : "Astronomy Pop Quiz", 
    "quiz" : "[{\"question\":\"Which of the following are currently (as of November 2015) used by scientists as observational evidence of the existence of dark matter?\",\"choices\":[\"Gravitational Lensing\",\"Specimens of dark matter collected by NASA\",\"Anomalies in planetary orbits\"],\"correct\":\"0\"},{\"question\":\"Which of these emits the most energy?\",\"choices\":[\"Stars\",\"Quasars\",\"Black Holes\"],\"correct\":\"1\"},{\"question\":\"What is it called when light or electromagnetic radiation from an object is increased in wavelength?\",\"choices\":[\"The Jupiter Effect\",\"Redshift\",\"The Observer's Differential\"],\"correct\":\"1\"},{\"question\":\"Who was the first human in space?\",\"choices\":[\"Yuri Gagarin\",\"Alan Shepard\",\"John Glenn\"],\"correct\":\"0\"},{\"question\":\"Which of these is the most dense?\",\"choices\":[\"The Sun\",\"A neutron star\",\"Earth\"],\"correct\":\"1\"}]", 
    "correctArray" : "[\"0\",\"1\",\"1\",\"0\",\"1\"]", 
    "author" : "Bertram", 
    "createTime" : ISODate("2016-09-14T04:29:52.636Z"), 
    "tags" : [ 
     "Astronomy" 
    ], 
    "likes" : 1, 
    "avgScore" : 0, 
    "plays" : 5, 
    "private" : "0" 
} 
{ 
    "_id" : ObjectId("57d8d3c173099cb013b462ba"), 
    "title" : "Film Trivia", 
    "quiz" : "[{\"question\":\"Who directed The Godfather trilogy?\",\"choices\":[\"John Huston\",\"Francis Ford Coppola\",\"Martin Scorsese\"],\"correct\":\"1\"},{\"question\":\"What year was the first Ocscar awarded?\",\"choices\":[\"1923\",\"1927\",\"1932\"],\"correct\":\"1\"},{\"question\":\"As of 2010, this and Schindler's List (1993) are the only films to win Best Picture, Director and Screenplay at the Golden Globes, BAFTAs and the Oscars.\",\"choices\":[\"Rain Man\",\"Slumdog Millionaire\",\"Titanic\"],\"correct\":\"1\"},{\"question\":\"In Casablanca, why can't Rick return to America?\",\"choices\":[\"He is indebted to the mob.\",\"He was deported.\",\"No reason is given.\"],\"correct\":\"2\"},{\"question\":\"What was the highest-grossing Western of all time?\",\"choices\":[\"Django Unchained\",\"True Grit\",\"Dances with Wolves\"],\"correct\":\"2\"}]", 
    "correctArray" : "[\"1\",\"1\",\"1\",\"2\",\"2\"]", 
    "author" : "Pappy2", 
    "createTime" : ISODate("2016-09-14T04:36:17.950Z"), 
    "tags" : [ 
     "Movies" 
    ], 
    "likes" : 1, 
    "avgScore" : 0, 
    "plays" : 8, 
    "private" : "0" 
} 
{ 
    "_id" : ObjectId("57ea7f67a58303f01a585e55"), 
    "title" : "US History Concepts", 
    "quiz" : "[{\"question\":\"\",\"choices\":[\"\",\"\",\"\"]}]", 
    "correctArray" : "[]", 
    "author" : "martha", 
    "createTime" : ISODate("2016-09-27T14:17:11.627Z"), 
    "tags" : [ 
     "US History", 
     "History" 
    ], 
    "likes" : 0, 
    "avgScore" : 0, 
    "plays" : 1, 
    "private" : "0" 
} 
+0

請發佈存儲在數據庫或架構中的文檔數據。 – Sachin

+0

@Sachin謝謝,我添加了所需的信息。 –

回答

1

你可以試試下面的聚集管道。

db.quizzes.aggregate([ 
         {"$unwind":"$tags"}, 
         {"$group":{"_id":"$tags", count:{$sum:1}}}, 
         {"$project":{"_id":0, "tags":{"tag":"$_id","count":"$count"}}}, 
         {"$group":{"_id":null, "tags":{"$push":"$tags"}}}, 
         {"$project":{"_id":0, tags:1}} 
        ]) 
+1

謝謝,這正是我正在尋找的東西。你如何按數字排序? –

+1

我把排序階段放在第一個小組階段之後就行了!再次感謝你的幫助。 –