DEV Community

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

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 !!