0

Is there any option to make exceptions for the unique property? I dont want MongoDB to set null if the value wasn't submitted or to make a exception for the null value in case of uniqueness.

userSchema = mongoose.Schema({
    /* ... additional Schema properties ... */
    email: {type: String, lowercase: true, unique: true}
});

It would be nice and easy if I could set the exception in the mongoose.Schema. Obviously, every other solution is appreciated as well. Thanks for your time! It helps me a lot. :)

1 Answers1

0

Am how about docs? Set validation on that field. And it will actually return you error if data dont pass validation.

http://mongoosejs.com/docs/validation.html

Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24
  • I found many options to limit possible submitted data, but unfortunately I didn't find any possibility for null to beeing ignored from the unique property – dunnohowishouldnamemyself Aug 03 '16 at 21:12
  • Well..Your validation method for email field will execute before save. And it will not save data until it pass true test but instead return error. Which you can send to user or save into logs. Look at examples on docs. Just try add validation into email field and try to save with null field. – Mykola Borysyuk Aug 03 '16 at 21:16