Question 49 / 50
Good to KnowMediumScenarioMongoose
An order list API uses populate('user') and populate('items.product') and is slow. What is going on, and what would you change?
Think about it first.
Short answer
populate() is extra queries (or a buffered join) per relationship, often on every row of the list. For a list, snapshot the fields you need on the order, project tightly, use lean(), and populate only on the detail page — or replace populate with a targeted $lookup after $limit.
Why?
populate() is convenient and easy to overuse. N orders with two populates can become many round trips. The modelling fix is the same as $lookup overuse: if you always need product name and price on the order, they should already be on the line item.
Interview tip
Connect populate() to $lookup. Same join, different API, same cost model.
Common mistake
Adding .populate() in a plugin so every find pays for it.
How did you do?
Cheat Sheet