Skip to content
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

The default value of "minimize" option should be false #8024

Open
huangxuewu opened this issue Jul 26, 2019 · 5 comments
Open

The default value of "minimize" option should be false #8024

huangxuewu opened this issue Jul 26, 2019 · 5 comments
Labels
backwards-breaking discussion If you have any thoughts or comments on this issue, please share them! performance
Milestone

Comments

@huangxuewu
Copy link

huangxuewu commented Jul 26, 2019

Do you want to request a feature or report a bug?

The Default value of the minimize option should be false.

What is the current behavior?
The default value is true.

If the current behavior is a bug, please provide the steps to reproduce.

This default behavior will remove empty Object without any notice.

What is the expected behavior?
Empty object should remove only when is intented to do so.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
mongoose 5.6.6
DB 4.0.10

@vkarpov15
Copy link
Collaborator

The reason for the minimize option being true is that Mongoose adds {} for all nested paths by default:

// Without minimize, you would always get `nested: {}`
const schema = Schema({ nested: { name: String } });

We'll close this for now because making this change would be very risky and not provide much benefit, but thanks for the suggestion 👍

@juona
Copy link

juona commented Dec 21, 2023

Bumped into this.

Perhaps ideally these should not be related? Or rather, the described behavior seems like a defect. nested is an optional field, and so is name, so it appears to be valid to provide either {} or { nested: {} }. If I don't provide a value for nested then nothing should be saved in the DB.

In other words, to me neither behaviour is expected - whether it's adding or removing empty subdocuments.

@vkarpov15
Copy link
Collaborator

I think we should reconsider setting minimize to false for Mongoose 9, but for different reasons. In save(), we need to copy the object using $toObject() to apply minimize. If we didn't need to apply minimize, we could just make this line use _doc, which would skip a potentially unnecessary cloning of the full document and lead to substantial performance gains on both #14394 and #14897.

@AbdelrahmanHafez @hasezoey do you have any thoughts on this?

@vkarpov15 vkarpov15 added this to the 9.0 milestone Sep 25, 2024
@vkarpov15 vkarpov15 added the discussion If you have any thoughts or comments on this issue, please share them! label Sep 25, 2024
@hasezoey
Copy link
Collaborator

i am partial to this as i have not encountered any problems with the current default (true) myself, though i can understand that when coming to mongoose and setting it to a value (even if empty) to not be saved (or expecting the property to exist at all in the saved document (like via mongodb compass)) could be unexpected.

btw is the statement from 2019 still true?:

The reason for the minimize option being true is that Mongoose adds {} for all nested paths by default:

@vkarpov15
Copy link
Collaborator

Yes the 2019 statement is still true. If you have a User model with new Schema({ name: { first: String, last: String }), const user = new User({}) will still have an empty object for user.name. That's because you cannot have getters and setters on null or undefined, so if we initialized user.name to null then we would need to do extra work to support change tracking on user.name = {}; user.name.first = 'John';.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards-breaking discussion If you have any thoughts or comments on this issue, please share them! performance
Projects
None yet
Development

No branches or pull requests

4 participants