MongoDb 查詢並顯示集合中重複的資料
阿新 • • 發佈:2018-11-10
例如有以下集合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} } }
])
查詢結果: