-
-
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
Introduce a simpler way to "populate unless populated" #14979
Comments
@vkarpov15 I want to work on this feature please assign me. |
I agree with the point that
I think I prefer option (1), and likely switch What do you think @iliakan ? |
@vkarpov15 The option flag looks like a good solution. The good thing about it is that a global default setting can be added as well. So people don't need to wait till Mongoose 9, they can set e.g. Will the collection variant look like this |
I was thinking we would just support Agree 💯 on the global option. |
I believe, both Model and instance methods deserve to have the "proper" Also, both instance and Model populate should work in the same way. |
…opulating already populated docs Fix #14979
@vkarpov15 looks good to me, I'm not familiar with Mongoose internal population mechanics, but the tests seem ok. Thank you. |
🚀 Feature Proposal
Is there a method to
populate
only if the value isn't already there? If not, maybe it's worth adding?Motivation
Currently, a
populate
call (both on a Model and an instance) queries the database, even if the value is already populated.However, it's quite common that objects come from different code paths, and they may have paths populated already. So, performance-wise, we should not do it again.
For a single instance, we can write
...But it's cumbersome to repeat this often.
Also, for an array of
users
we need to check each item, which makes even longer code compared to a single call, e.g.User.populateMissing(users, 'friends')
, which could work just asUser.populate
(single query to fetch all users), but omit already-populated paths.Additional thoughts
To say the truth, after many years of development with Mongoose, my experience is that the current behaviour (always querying the database) is much less needed than the lazy one (query only if populated path doesn't exist).
The text was updated successfully, but these errors were encountered: