How to remove duplicates based on a key in Mongodb?

后端 未结 8 830
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 20:56

I have a collection in MongoDB where there are around (~3 million records). My sample record would look like,

 { \"_id\" = ObjectId(\"50731xxxxxxxxxxxxxxxxxx         


        
8条回答
  •  青春惊慌失措
    2020-11-30 21:06

    Here is a slightly more 'manual' way of doing it:

    Essentially, first, get a list of all the unique keys you are interested.

    Then perform a search using each of those keys and delete if that search returns bigger than one.

        db.collection.distinct("key").forEach((num)=>{
          var i = 0;
          db.collection.find({key: num}).forEach((doc)=>{
            if (i)   db.collection.remove({key: num}, { justOne: true })
            i++
          })
        });
    

提交回复
热议问题