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

fix(populate): merge instead of overwrite when match is on _id #12891

Merged
merged 1 commit into from
Jan 10, 2023

Conversation

vkarpov15
Copy link
Collaborator

Fix #12834

Summary

#12834 points out that match on _id overwrites rather than merges. Which is not a very good API design, because any custom match that specifies _id will overwrite the _id filter specified by populate(). This PR fixes that.

Examples

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! 👍
I have no idea how I survived not being bitten by this bug all those years. 😄

@vkarpov15
Copy link
Collaborator Author

@AbdelrahmanHafez do you find yourself using match with _id? I've never done that before, although I can see why it would be tempting.

@vkarpov15 vkarpov15 merged commit 6e14876 into master Jan 10, 2023
@AbdelrahmanHafez
Copy link
Collaborator

Now that I think about it, I don't think I ever did use match with _id, but I can see a scenario where I would want to find an intersection/subset between documents coming from one place and matching them in a populate.

@Uzlopak Uzlopak deleted the vkarpov15/gh-12834 branch January 10, 2023 11:37
@yassine-cc
Copy link

Hey @vkarpov15 , i'm the one who created the issue related to this, thank you for fixing it.

I just want to point out that the test case in the PR wouldn't catch this bug as only the ids that are present get populated
and so the issue wouldn't affect the length of the populated array inside the document.

There is a difference between the query results returned by the db and the result that mongoose produces.

The bug is more about the performance impact of getting a huge query result size back from the db (huge memory usage), instanciating every item in the array, and then iterating over them to match them with the ids inside the array to populate

@vkarpov15
Copy link
Collaborator Author

@yassine-cc can you please clarify the issue you're seeing? I unfortunately don't understand what you mean by "only the ids that are present get populated and so the issue wouldn't affect the length of the populated array inside the document"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected behavior with the populate method leading to memory issues
3 participants