-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
defaults
option for queries
#7287
Comments
This is something worth discussing more. Currently, this is by design. The spirit of this behavior is to apply defaults where possible, because You can always use |
@vkarpov15 i kinda agree that it's more writing when you need to null-check attributes and can't rely on it beeing an empty string or so.
|
Any updates on this issue? We sometimes deliberately put empty objects to the database and count on them to be empty when we fetch them. But they are populated with the default values. |
@GirayEryilmaz you can use We'll add a new option to disable defaults on queries, since we already have a |
defaults
option for queries
TLDR; |
Hello, The default behaviour may be helpful for values like booleans. I think the default behaviour should be reversed. And we should be able to fill in defaults on demand when reading. uuid: {
type: String,
default: () => uuid(),
setDefaultOnRead: false,
}, |
@hugomallet I don't understand your issue, can you please clarify? |
@vkarpov15, my case was : I added a new field scheduledAt to an existing model. This fields defaults to the current date at the time of the insert if not defined. scheduledAt: {
type: Date,
default: () => new Date(),
}, My issue is that the previous objects have an undefined scheduledAt field. But when i read, scheduledAt equals to the current date (read time) which is an unwanted value because it changes at every read. I expected to have undefined instead of an incorrect value. |
@hugomallet you can create a middleware that sets schema.pre(['find', 'findOne'], function disableDefaults() {
this.setOptions({ defaults: false });
}); Would that help? |
Hi, @vkarpov15 Thanks for adding In addition to |
Doesn't seems to work on me. Thoughts? I have the exact same issue as hugomallet |
@junjenfong please open a new issue and follow the issue template. "Doesn't work" isn't an actionable bug report. |
Do you want to request a feature or report a bug?
Possible bug or unexpected behaviour
What is the current behavior?
.find()
overwrites fields that areundefined
but have adefault
value with that exact value, making it inconsistent and not possible to see if such that value is set in the database or not.See #7269 for reference.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
The default value of an attribute should be used on creation or maybe even on
update
(https://mongoosejs.com/docs/defaults.html#the-setdefaultsoninsert-option) but NOT on pure reading a document from the database.
Please mention your node.js, mongoose and MongoDB version.
node v8.11.3, mongoose 5.3.13, mongoDB latest version
The text was updated successfully, but these errors were encountered: