0

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.

1 Answers1

1

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

Borislav Gizdov
  • 1,323
  • 12
  • 22