使用聚合框架按关键字段查找MongoDB集合中的所有重复文档。
为了理解这个概念,让我们用文档创建一个集合。使用文档创建集合的查询如下-
> db.findDuplicateByKeyDemo.insertOne({"StudentId":1,"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f5b168d10a061296a3c3a") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":2,"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f5b1f8d10a061296a3c3b") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":3,"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f5b248d10a061296a3c3c") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":4,"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f5b2d8d10a061296a3c3d") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":5,"StudentName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f5b398d10a061296a3c3e") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":6,"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c7f5b438d10a061296a3c3f") }
在find()
method的帮助下显示集合中的所有文档。查询如下-
> db.findDuplicateByKeyDemo.find().pretty();
以下是输出-
{ "_id" : ObjectId("5c7f5b168d10a061296a3c3a"), "StudentId" : 1, "StudentName" : "John" } { "_id" : ObjectId("5c7f5b1f8d10a061296a3c3b"), "StudentId" : 2, "StudentName" : "Carol" } { "_id" : ObjectId("5c7f5b248d10a061296a3c3c"), "StudentId" : 3, "StudentName" : "Carol" } { "_id" : ObjectId("5c7f5b2d8d10a061296a3c3d"), "StudentId" : 4, "StudentName" : "John" } { "_id" : ObjectId("5c7f5b398d10a061296a3c3e"), "StudentId" : 5, "StudentName" : "Sam" } { "_id" : ObjectId("5c7f5b438d10a061296a3c3f"), "StudentId" : 6, "StudentName" : "Carol" }
这是查找所有重复文档的查询”-
> db.findDuplicateByKeyDemo.aggregate([ ... { $group: { ... _id: { StudentName: "$StudentName" }, ... UIDS: { $addToSet: "$_id" }, ... COUNTER: { $sum: 1 } ... } }, ... { $match: { ... COUNTER: { $gte: 2 } ... } }, ... { $sort : { COUNTER : -1} }, ... { $limit : 10 } ... ]).pretty();
以下是显示重复记录的输出。在这里,学生“ Carol”来了3次,而John则是2次-
以下是输出-
{ "_id" : { "StudentName" : "Carol" }, "UIDS" : [ ObjectId("5c7f5b248d10a061296a3c3c"), ObjectId("5c7f5b438d10a061296a3c3f"), ObjectId("5c7f5b1f8d10a061296a3c3b") ], "COUNTER" : 3 } { "_id" : { "StudentName" : "John" }, "UIDS" : [ ObjectId("5c7f5b2d8d10a061296a3c3d"), ObjectId("5c7f5b168d10a061296a3c3a") ], "COUNTER" : 2 }