1

I am trying to make email as unique. I tried using 'unique' and 'dropDups', but it doesn't work. Any idea what I should use?

var custSchema = new Schema({ 
  "username": String,
  "Password": String,
  "email": {["Type": String,
             "Value": { type : String, lowercase: true, unique: true, dropDups: true}]
})  

Example of csv data:

        username       Password            Email
                                      Type     Value
User1:   test1        asdub**jhas    Primary   test1@user1.com
User2:   test2        3kjnn**8man    Primary   Pract@test.com
User3:   test3        dsffs**as97    Primary   test1@user1.com

In this example case, there would be only two users added to the database: User1 and User2, because User3 has the same email address as User1.

Thanks for helping.

2 Answers2

0

Create an Index for email.Value with option unique: true and this will help us in preventing the insert of duplicate values for email Id.

db.collection_name.createIndex({"email.Value":1}, {unique:true})

References:

CreateIndex

Another SO Question

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
0

There might be some other solution that can fix this. But after I tried defining 'Email' as

"Email": [
{"Type": String,
  "Value": { type : String, lowercase: true, index: {unique: true, dropDups: true}}
}]

it works fine.