Git Product home page Git Product logo

Comments (6)

paradoxmr24 avatar paradoxmr24 commented on May 26, 2024 2

mongoose don't run validators on update by default, if you want to run validators on update operations also then you need to write this:

mongoose.set('runValidators', true);

from mongoose.

vkarpov15 avatar vkarpov15 commented on May 26, 2024 1

Update validators are disabled by default because update validators come with some caveats that tend to cause trouble for custom validators. Read these sections of the validation docs for more info on the caveats.

so should i preferably use save instead update to ensure data i am trying to add in db matches schema for sure? Yes, if matching your schema as closely as possible is a priority then you should use save(). In general, we recommend using save() over updateOne() and findOneAndUpdate() unless you need extra performance or more fine grained concurrency control.

how does it work with strict=throw? With strict: 'throw', updates will throw an error if you try to set a field that isn't in your schema.

from mongoose.

vkarpov15 avatar vkarpov15 commented on May 26, 2024 1

@vasyl97kharchenko that sounds like a potential bug, runValidators: true should prevent explicit $unset of required fields. Can you please provide a script that demonstrates the issue you're seeing?

from mongoose.

vasyl97kharchenko avatar vasyl97kharchenko commented on May 26, 2024 1

@vkarpov15 false alarm, did a double check, if runValidators: true -> it's not possible. Behaviour with $unset is only possible when validators are not running.

from mongoose.

MikeKoval avatar MikeKoval commented on May 26, 2024

why its disabled by default?
what does "Be careful: update validators are off by default because they have several caveats." really mean according to documentation?
so should i preferably use save instead update to ensure data i am trying to add in db matches schema for sure?
how does it work with strict=throw? i think without having that validation enabled by default strict mode might throw an error, right?

from mongoose.

vasyl97kharchenko avatar vasyl97kharchenko commented on May 26, 2024

@vkarpov15 is it still ok that because of this behavior I can even delete required fields using $unset operator during update (found out it today)?

from mongoose.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.