Populate feild then find
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal
I have the following tables:
And I want to make a query, and filter inside the subscriptions’ collection only for all the users who have the role “client”.
So the query should return:
[
{
id: '1',
name: 'Ahmad',
role: 'client',
balance: 0
}
]
How to filter in MongoDB for this?
The only solution, unfortunately, is to use .aggregate()
with $lookup
, then $match
, then $unwind
, a repetitive pattern that always happen for this scenario.
Motivation
Here’s a similar problem that was solved using aggregate: https://stackoverflow.com/a/74580085/18387350
it has many drawbacks, and it’s not clean and makes the code long.
Example
Instead of making Subscription.find().populate()
, let us do the opposite Subscription.populate().find()
.
Subscription.populate('client').find({
'client.branch': 'BRANCH_ID'
})
- this should mean, populate first, then filter.
Issue Analytics
- State:
- Created 10 months ago
- Reactions:4
- Comments:6
Top Results From Across the Web
Return certain fields with .populate() from Mongoose
Show activity on this post. If you only want a few specific fields to be returned for the populated documents, you can accomplish...
Read more >Mongoose v6.8.2: Query Population
You can call the populated() function to check whether a field is populated. If populated() returns a truthy value, you can assume the...
Read more >Auto-populate the Lookup Field with Process Builder
Auto-populate the Lookup Field with Process Builder. ... From Setup, enter Builder in the Quick Find box, and select Process Builder.
Read more >How to use Populate in Mongoose & Node.js
If no document is found to populate, then field will be null . · In case of array of documents, if documents are...
Read more >auto populate values based on another field value - ServiceNow
Solved: I have a field which is referenced to " sys_user " table . If i select a value from user table then...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Well i strongly doubt that this is fast. You will complain in the future that your operation is slow. Than you either want to change to a SQL-Server or you want to keep mongo but need to invest time to restructure the data to improve the performance.
Also short reminder what a population on mongoose is. When populating a field mongoose loads the data from the database collection and stores it in memory and replaces the corresponding fields of the parent with the documents.
So this means with your proposol to do first populate then find, that you have basically load both, the parent collection and the referenced collection, and then filter. This obviously wont work for big collections.
If you want to achive later some kind of pagination, you would be forced to definetly load the whole collections, as sorting would be only possible in nodejs.
It is one of the principles of working with databases to let the database do the work and not the logic.
I strongly recommend to read about organizing data in nosql databases.