Mongoose: findOneAndUpdate not respecting enum values

Created on 5 Feb 2018  路  1Comment  路  Source: Automattic/mongoose

mongoose version: 5.0.1
MongoDB version: 3.6.2
node.js version: 9.3.0

When saving, the enum validation works as expected but when updating using the findOneAndUpdate method the enum validation does not work.

Almost exactly the same problem as described here, the only difference is that I'm using $addToSet: https://github.com/Automattic/mongoose/issues/860#issuecomment-168012110

If this is not possible yet, which is the recommended way to validate enums on update?

Most helpful comment

I just found out that the only thing necessary to make it work is passing the runValidators: true in the options of the findOneAndUpdate method, like this:

const order = await Order.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, runValidators: true});

>All comments

I just found out that the only thing necessary to make it work is passing the runValidators: true in the options of the findOneAndUpdate method, like this:

const order = await Order.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, runValidators: true});

Was this page helpful?
0 / 5 - 0 ratings