我的 MongoDB 数据库中存储了以下文档(假设是 collection products
):
{
a: "test1",
c: "data1"
},
{
a: "test2",
c: "data2"
},
{
a: "test3",
c: "data1"
},
{
a: "test4",
c: "data3"
},
{
a: "test5",
c: "data1"
},
{
a: "test6",
c: "data3"
},
如何查询所有属性c
重复(或三次重复...)的文档?在示例数据中,查询应返回test1, test3, test4, test5 and test6
文档。
您可以通过分组c
然后获取组中超过一个的组来做到这一点:
db.test.aggregate([
{$group: {_id: '$c', count: {$sum: 1}, docs: {$push: '$$ROOT'}}},
{$match: {count: {$gt: 1}}}
])
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句