How to remove duplicates based on a key in Mongodb?

后端 未结 8 833
伪装坚强ぢ
伪装坚强ぢ 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:24

    If you have enough memory, you can in scala do something like that:

    cole.find().groupBy(_.customField).filter(_._2.size>1).map(_._2.tail).flatten.map(_.id)
    .foreach(x=>cole.remove({id $eq x})
    

提交回复
热议问题