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.