Reputation: 2006
I have tons of data in my database. and i have so many duplicates in it. I want to remove all these duplicates and keep only one data from it,
For example i have data like this in my login object
id name
123 abc
124 abc
125 abc
126 abc
127 pqr
128 pqs
i want to keep only one user whose name is 'abc' and remove all the other users whose name is 'abc' How can i achieve it in mongodb thanks in advance
Upvotes: 1
Views: 189
Reputation: 43884
One method is to actually create a unique index on the users name and give it an option to dropDups: http://docs.mongodb.org/manual/core/indexes/#drop-duplicates
So for your example:
db.user.ensureIndex({name:1},{unique:1,dropDups:true})
Would create a unique index on the name that will drop all other duplicates.
Of course the double benefit of this is that you now have a unique index on the name ensuring that you don't get duplicates again.
You can also do this via clent side but it is likely to be slower than doing it by building an index.
Upvotes: 1