Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add unique index to mongo collection with duplicates?

Tags:

mongodb

I'm a bit new with mongo and I would like to know the best way to handle the following situation.

I've a students collection that has a field named email, the collection already contains some records with this property set and we've found that there are some records with the email duplicated (more than one student have the same email). So from now we would like to convert the email property to an unique index, so I was wondering what is the process for it.

Should I remove first the existent duplicates or is it enough to add the unique index now and it will ensure that from now the collection (db) doesn't allow them to create records with existent emails?

I mean, we don't have problem with the existing/earlier records, but we would like to prevent it from happening for future records.

like image 415
Alfredo Esteban Hernndez Dvalo Avatar asked Dec 21 '25 09:12

Alfredo Esteban Hernndez Dvalo


1 Answers

To add a unique index you can use:

db.collection.createIndex( email, { unique: true } )

A unique index ensures that the indexed fields do not store duplicate values; i.e. enforces uniqueness for the indexed fields. By default, MongoDB creates a unique index on the _id field during the creation of a collection.

https://www.mongodb.com/docs/manual/core/index-unique/

However, you have to remove documents with duplicate email values before that

db.dups.aggregate([{$group:{_id:"$email", dups:{$push:"$_id"}, count: {$sum: 1}}},
{$match:{count: {$gt: 1}}}
]).forEach(function(doc){
  doc.dups.shift();
  db.dups.remove({_id : {$in: doc.dups}});
});

Check this answer for more info https://stackoverflow.com/a/35711737/1278463

like image 99
Borislav Gizdov Avatar answered Dec 24 '25 02:12

Borislav Gizdov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!