Suggestion: a method to find many documents by Id #9947
meircarlos
started this conversation in
Ideas
Replies: 1 comment 3 replies
-
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I feel that mongoose is missing a basic method that given an array of Ids, returns an array of the same length, in which at each position is the database document or undefined if it doesn't exist.
This is a really basic an interesting method as it allows not only to easily retrieve and know which Ids exist in the database, but also having the values in the same index position of the resulting array can be quite useful for development.
I also believe this method to be necessary as a consequence of many questions that people have made related to this, in which they have been instructed to do the following:
When it can be much more easier and useful to do something like:
Spoiler alert: what makes this useful is that they would not work in exactly the same way, meaning that the
find
method works more like an SQL query, as the documentation describes, but how I see it the purpose of this method would be to simulate what thepopulate
method already does really nicely by populating all the documents of an array of ids. However there is no easy approach for doing this with a simple array of ids.I know that this could just be implemented as a plugin for all my models, however I'm just stating it here as a suggestion for mongoose and in case anyone else might be interested, my method looks like this:
Beta Was this translation helpful? Give feedback.
All reactions