0

I imported a lot of records and need to be able to delete duplicates that might have been imported by mistake.

enter image description here

On a separate note, I would like to be able to query all records for specific keywords. I am new to MongoDB and was hoping someone could help with a query or two.

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30
Arsenii
  • 109
  • 1
  • 4
  • 8
  • Possible duplicate of [Fastest way to remove duplicate documents in mongodb](https://stackoverflow.com/questions/14184099/fastest-way-to-remove-duplicate-documents-in-mongodb) – Nilesh Singh Jan 27 '18 at 20:39

1 Answers1

0

In order to remove duplicates based on a key, you can create an index on the collection and enable dropDups like this,

db.yourCollection.ensureIndex({'myKey' : 1}, {unique : true, dropDups : true})

The following index will keep the first unique document and drop any duplicates followed by that.

Note: dropDups will not work in MongoDB 3.0 or above. If you're a new version, please follow this solution here instead.

As to query records for specific keywords, you can use both find (with or without regex) and MongoDB's text search.

You can find more information on MongoDB find here and on Text Search here.

Nilesh Singh
  • 1,750
  • 1
  • 18
  • 30