Skip to content Skip to sidebar Skip to footer

Mongodb Query To Remove Duplicate Documents From A Collection

I take data from a search box and then insert into MongoDB as a document using the regular insert query. The data is stored in a collection for the word 'cancer' in the following f

Solution 1:

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')

notes:

  • 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.

Post a Comment for "Mongodb Query To Remove Duplicate Documents From A Collection"