Learn › Advanced aggregation
Joining collections with $lookup
The basic join, the pipeline form, and what it costs.
By the end: Pull related documents from another collection into your result.
Lessons
- $lookup, and why it returns an array
MongoDB $lookup is the join: pull matching documents from another collection. It always returns an array, even for one match.
- $unwind after $lookup
Pairing $unwind with $lookup in MongoDB to turn the joined one-element array into a plain embedded object.
- Trimming the joined document
Trim a MongoDB $lookup result with $project so you return two or three joined fields instead of a whole document.
- Pipeline $lookup with let and $expr
MongoDB pipeline $lookup with let and $expr: filter the joined collection during the join, not after it.
- What $lookup costs
What $lookup actually costs in MongoDB, why it runs per document, and when embedding beats joining.