且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MongoDB查询以从集合中删除重复的文档

更新时间:2023-02-14 08:21:31

在mongo shell中的简单解决方案:`

an easy solution in mongo shell: `

use your_db
db.your_collection.createIndex({'1': 1, '2': 1, '3': 1, etc until you reach maximum expected letter count}, {unique: true, dropDups: true, sparse:true, name: 'dropdups'})
db.your_collection.dropIndex('dropdups')

注释:

  • 如果您有许多文档,则希望此过程花费很长时间 时间
  • 注意,这将删除现有文档,***先克隆您的收藏集,然后在此处尝试.
  • if you have many documents expect this procedure to take very long time
  • be careful this will remove documents in place, better clone your collection first and try it there.