-
-
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
issue with modifying unselected subdocs #9427
Comments
@dietergeerts can you please add code samples that demonstrates the issue you're seeing? Also, why is the subdocument deselected? Perhaps you could work around this issue by projecting the subdocument in for the code in question? The issue is that, if the field is deselected, Mongoose doesn't have a previous value to diff against, so it doesn't know what changed. That's why Mongoose is overwriting the entire subdocument. |
@vkarpov15 , as the code about is in a middleware, it doesn't know what will be selected. It could add its own selects when a query is done, but then these selects will always be done, even if those fields doesn't need to change. The changes done on the data by the plugin aren't always done, it depends on other data being changed, and as we tested the performance of mongoose and selects, we noticed that it performs so much quicker the less selected paths there are. That's why at first, we didn't want to select these paths if they aren't always changed. A possible solution would be to have an option with |
Copied from #4651 (comment) from @dietergeerts
I just encounter this bug in version 5.9.16. Does this mean that this got reverted, and when doing changes in a sub-document, it has to be selected in the first place?
I wrote a plugin that does some extra changes based on other changes. The plugin is sure that the sub-documents exist, and it wants to update 1 specific field. Though using this.set completely overwrites the whole sub-document, which is not expected. Even though it was not specifically selected, and it conforms to the schema, I would expect only that path to be updated, not the whole sub-document. I must say that this is in a sub-document array.
A workaround I've found that makes my unit tests green is:
Though I don't know if using unmarkModified will have some side-effects that we don't want?
Imho, it makes sense to set certain paths that aren't selected, but are defined in the schema. If any error happens because the data doesn't exist, then it's our own fault off course, and then the unhappy business logic path wasn't taken into account.
The text was updated successfully, but these errors were encountered: