I am trying to make email field as unique on database level. When a new document is created with same emailId it should throw an error.
To implement it I am using
email : {type: String, trim: true, index: true, unique: true, sparse: true,required: true}
But it doesn't seem to be working , currently it is adding new documents with same emailId.
Referred this post : mongoDB/mongoose: unique if not null
Checked .getIndexes()
and this is the output of it :