DEV Community

Discussion on: How to use Populate in Mongoose & Node.js

Collapse
 
hemantparmar profile image
hemant-parmar

Hi Paras,

This was a nice read. I am going to implement this for my MEAN project. A Couple of questions.

I have a Log collection, which has 4 ObjectId type fields. i.e. client, service, executive, manager - of course apart from its own fields. And when on frontend, the Log is displayed, I am planning to populate all these 4 (i.e. Client Name, Category, Sub Category, Rating), (Service Name, Service Freq), ExecName and ManagerName.
The Log display on frontend has Search and filter options on various fields. So when a user searches or applies filters the backend Mongoose query will run again.

What would be the performance impact if the number of entries in Log collection is in the range to 5000 - 50,000?

For the above type of case would there be any other option for fetching the related data OR this is the best option.

Collapse
 
paras594 profile image
Paras 🧙‍♂️

Though populate is not bad. It is optimised. But there is a another way. If you know about aggregation framework in mongodb then there is a $lookup option that you can add in pipeline. I am still learning about aggregation framework. I have read that $lookup is faster than populate. Try to find more about it

Just know one thing. That populate makes a second call to database. So in your case 4 populates = 4 calls.

Or, you can do a live test :p...run query with populate and check its performance then run query with $lookup pipeline and compare its performance. (mongodb has ways to check query performance)

I hope it helps you. All the best for your project :)

Collapse
 
nrmad profile image
nrmad

$lookup cannot be sharded and so it limits your scaling, super annoying constraint because I loved this stage until I spotted that.

Thread Thread
 
paras594 profile image
Paras 🧙‍♂️

Okayy. thank you !! I didn't know that :)
I still have to learn more about sharding. Never tried it.

Collapse
 
hemantparmar profile image
hemant-parmar

Thanks Paras for your quick response. It surely helps.

Thread Thread
 
paras594 profile image
Paras 🧙‍♂️

You are welcome sir !!