BREAKING CHANGE: call virtual ref function with subdoc, not top-level doc
#14652
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix #12363
Fix #12440
Summary
The primary issue from #12363 and #12440 is that right now we call
ref()functions on virtuals with the top-level document rather than the subdocument, even if the virtual is defined on the subdocument. For example, in Mongoose 8, with the following setup,ref()would seethisas an instance ofParentSchema, not thebsubdocument.With this PR, we will call
ref()with the subdocument. This is more consistent with how things work in conventional populate:ref()would get called with the subdoc in virtual populate, see #8469 and this test.Examples
If you're using
ref()as a function, it is easier to get the top-level document from the subdocument (usingsubdoc.ownerDocument()) than it is to get the correct subdocument from the top-level document. Plus, callingref()with the subdocument helps make schemas easier to reuse.