querydrill

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

5
  1. $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.

  2. $unwind after $lookup

    Pairing $unwind with $lookup in MongoDB to turn the joined one-element array into a plain embedded object.

  3. Trimming the joined document

    Trim a MongoDB $lookup result with $project so you return two or three joined fields instead of a whole document.

  4. Pipeline $lookup with let and $expr

    MongoDB pipeline $lookup with let and $expr: filter the joined collection during the join, not after it.

  5. What $lookup costs

    What $lookup actually costs in MongoDB, why it runs per document, and when embedding beats joining.

Drills

2 medium · 1 hard
Start reading →Skip to the drills