Mongoose findOneAndUpdate and runValidators not working
required validators only fail when you try to explicitly $unset the key.
This makes no sense to me but it's what the docs say.
use this plugin:mongoose-unique-validator
When using methods like findOneAndUpdate
you will need to pass this configuration object:
{ runValidators: true, context: 'query' }
ie.
User.findOneAndUpdate( { email: 'old-email@example.com' }, { email: 'new-email@example.com' }, { runValidators: true, context: 'query' }, function(err) { // ...}
In mongoose do same thing in two step.
- Find the result using
findOne()
method. - Add fields and save document using
Model.save()
.
This will update your document.