純前端一枚,之前沒有接觸后臺數據庫,現在學習mongodb數據庫,遇到這樣的一個情景,建立一個博客的數據庫,有這樣一個包含所有文章信息的集合,數據偽造如下:{ "id" : 1, "name" : "one", "tags" : [ "a", "c", "e" ] }
{ "id" : 2, "name" : "two", "tags" : [ "e" ] }
{ "id" : 3, "name" : "three", "tags" : [ "d", "e" ] }
{ "id" : 4, "name" : "four", "tags" : [ "g", "c", "e", "h" ] }
{ "id" : 5, "name" : "five", "tags" : [ "a", "c", "d" ] }tags 表示這篇文章所屬的標簽類型,現在想要查詢出有多少個不同的標簽類型,每個標簽下有多少條數據,這個該怎么實現?目前想到的是用 aggregate 中的 $unwind 將 tags 進行拆分,然后再用 $group 進行分組統計,但是具體代碼該怎么寫,或者是有其他更有效的方式,請指教!?。?
關于mongodb聚合查詢的問題·?
qq_遁去的一_1
2018-07-13 19:07:23