例如有以下集合users:
{ "_id" : 1, "openid" : "ojQrH5X-V1asIS7uAk2iL-m6azro" },
{ "_id" : 2, "openid" : "ojQrH5dwrqiv3O4zgZLWp43dBAa4" },
{ "_id" : 3, "openid" : "ojQrH5T4lgXm7Fbhyt1ytdrNy9Kg" },
{ "_id" : 4, "openid" : "ojQrH5X-V1asIS7uAk2iL-m6azro" },
{ "_id" : 5, "openid" : "ojQrH5d163NDRoXoGNbenTE17BDw" },
{ "_id" : 6, "openid" : "ojQrH5dwrqiv3O4zgZLWp43dBAa4" },
{ "_id" : 7, "openid" : "ojQrH5d163NDRoXoGNbenTE17BDw" },
{ "_id" : 8, "openid" : "ojQrH5dwrqiv3O4zgZLWp43dBAa4" }
查找集合中有重复的openid,显示重复的openid和重复的个数
db.getCollection('users').aggregate([ { $group: { _id : '$openid', count: { $sum : 1 } } }, { $match: { count: { $gt : 1} } }])
查询结果:
原文链接:https://blog.csdn.net/qq_23375733/article/details/83147854