We only need to group by 2 keys and select elements with a number greater than 1 to find duplicates.
Request : - It will be like
db.mycollection.aggregate( { $group: { _id: { foreing: "$foreing", value: "$value" }, count: { $sum: 1 }, docs: { $push: "$_id" } }}, { $match: { count: { $gt : 1 } }} )
OUTPUT : - It will be like
{ "result" : [ { "_id" : { "foreing" : 1, "value" : 2 }, "count" : 2, "docs" : [ ObjectId("34567887654345678987"), ObjectId("34567887654345678987") ] } ], "ok" : 1 }
Link Link: - How to find documents with the same field
Nishant
source share