Mongoose findOneAndUpdate and runValidators not working Mongoose findOneAndUpdate and runValidators not working mongoose mongoose

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.

  1. Find the result using findOne() method.
  2. Add fields and save document using Model.save().

This will update your document.