Find duplicate records in MongoDB

前端 未结 4 1942
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 02:14

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\         


        
4条回答
  •  心在旅途
    2020-11-28 03:07

    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.
    • Then 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" ] }
    

提交回复
热议问题