Mongo - 与整个阵列完全匹配

时间:2017-10-11 16:37:20

标签: mongodb mongodb-query

我的文件看起来像这样:

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

我想调用distinct来获取所有唯一数组,但是,默认情况下查询数组匹配元素,这意味着:

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}

1 个答案:

答案 0 :(得分:2)

您可以在汇总管道中使用$group arr$sum

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