2017-10-11 96 views
0

我有一個看起來是這樣的文件:蒙戈 - 鮮明的匹配整個陣列

{"arr":["a","b","c"]}, 
{"arr":["a","b","x"]}, 
{"arr":["c","b","a"]}, 
{"arr":["a","b","c"]} 

我想不過來調用不同的讓所有的獨特陣列,查詢數組默認匹配的元素,這意味着這樣的:

db.getCollection("mycoll").distinct("arr",{}); 

回報:

["a","b","c","x"] 

代替

[["a","b","c"], 
["a","b","x"], 
["c","b","a"]] 

如何查詢不同的數組?或者,我很想得到每個結果的出現次數過多:

{["a","b","c"] : 2, 
["a","b","x"] : 1, 
["c","b","a"] : 1} 
+0

在聚合管道中使用'$ sum'在數組上使用'$ group'。就像'db.collectionname.aggregate({$ group:{_ id:「$ arr」,count:{$ sum:1}}})' – Veeram

+0

這對我有效:'db.mycoll.aggregate({$ group: {_id: 「$ ARR」,計數:{$總和:1}}})'。謝謝!如果你做出回答,我會接受它。 – Porthos3

+0

[Group by sum mongodb]可能重複(https://stackoverflow.com/questions/17909694/group-by-sum-mongodb) – Veeram

回答

1

您可以通過arr$sum聚集管道使用$group

db.mycoll.aggregate({$group:{_id:"$arr", count:{$sum:1}}})