How would I find duplicate fields in a mongo collection.
I\'d like to check if any of the \"name\" fields are duplicates.
{
\"name\" : \"ksqn291\
You can find the list of duplicate names using the following aggregate pipeline:
Group all the records having similar name.Match those groups having records greater than 1.group again to project all the duplicate names as an array.The Code:
db.collection.aggregate([
{$group:{"_id":"$name","name":{$first:"$name"},"count":{$sum:1}}},
{$match:{"count":{$gt:1}}},
{$project:{"name":1,"_id":0}},
{$group:{"_id":null,"duplicateNames":{$push:"$name"}}},
{$project:{"_id":0,"duplicateNames":1}}
])
o/p:
{ "duplicateNames" : [ "ksqn291", "ksqn29123213Test" ] }