Comments (6)
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.
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.
@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.
@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.
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.
@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)
- Nested populate with nested paths and subdocuments is not working fine HOT 6
- Best method to set Query and Lean defaults HOT 4
- Issue with 2dsphere indexes in nested schemas vs subdocuments HOT 2
- (Typescript) toObject on populated documents loses the structure
- Recompiling schema does not reapply discriminators HOT 1
- Using default value on sub documents does not work as expected HOT 3
- Recursion check in toObject breaks mongoose-intl plugin HOT 2
- Cannot define discriminator options on a Schema HOT 1
- Model.createSearchIndex() create SIGTERM on DuplicateIndex
- Populate doesn't work - nested path + wildcard + 2 references on a same model
- How to catch MongoNetworkError: connect ECONNREFUSED in Node when using mongoose.createConnection() HOT 2
- Pre and Post (middleware) hooks issue
- getters array in virtualtype for "id" virtual is not deduped HOT 1
- FilterQuery Typing fails for generic type HOT 5
- Change tracking is getting lost on transaction retry HOT 1
- Types break in semver patch change HOT 4
- "$inc" operator not working HOT 1
- Cannot find module 'mongoose' or its corresponding type declarations. HOT 4
- Type of subdocument not inferred in toJSON() HOT 2
- Differentiating Type when using `lean()` vs no `lean()` HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongoose.