advanced
Population
Resolve referenced documents while watching query count, projection, consistency, and whether embedding would be simpler.
Population replaces stored ObjectIds with documents from other collections via additional queries. Convenient for references; risky for query count and over-fetching.
await Order.find()
.populate({ path: 'customerId', select: 'name email' })
.lean();
On interviews: compare population with embedding, aggregation `$lookup`, manual batched queries, projections, and lean results. Mention cardinality and ownership checks.
Common pitfalls: deep population chains; population making MongoDB feel relational; missing projections and auth checks on referenced docs.
The trade-off is developer ergonomics versus explicit query design.
Checklist:
- Check cardinality before populating.
- Limit projected fields.
- Prefer embedding when aggregate ownership is clear.
- Measure query count per endpoint.