Stop 06 · MongoDB Data Modeling & AggregationTopics · 5/8

$lookup Joins

Part of the MERN Stack Roadmap

In short

When orders store a userId, sooner or later a report needs the customer's name next to each order. $lookup is the aggregation stage that pulls in matching documents from another collection, much like a SQL join.

$lookup is an aggregation stage that pulls matching documents from another collection into each result, which is MongoDB's way of joining referenced data.

The jaadu, as a teacher, holds one sheet in each hand and draws lines connecting matching rows between the two pages.

Think of it like this

Matching attendance with the student list

A teacher has a page of roll numbers who attended and a separate register with names. For each roll number she looks up the register and writes the name beside it. $lookup does that lookup for every document in your pipeline.

Watch it happen

Orders joined with users

$match
$lookup
$unwind
$project

1/4Keep only this month's paid orders. Filter early so the join does less work.

The big ideas

What you need to know

Four basic fields

from (other collection), localField, foreignField and as (name of the new array field).

Result is always an array

Even one match comes back as an array. Use $unwind or $first to flatten it.

Index the foreign field

Make sure foreignField is indexed. _id always is.

Pipeline form

$lookup can take its own pipeline to filter or project the joined documents, such as only the last 3 orders.

See it in code

Walk through the code

js
1db.orders.aggregate([2  { $match: { status: "paid", createdAt: { $gte: ISODate("2026-11-01") } } },3  {4    $lookup: {5      from: "users",6      localField: "userId",7      foreignField: "_id",8      as: "customer",9      pipeline: [{ $project: { name: 1, city: 1 } }],10    },11  },12  { $unwind: "$customer" },13  { $project: { total: 1, "customer.name": 1, "customer.city": 1 } },14  { $sort: { total: -1 } },15]);

1/5Filter first, so only relevant orders are joined.

This month's paid orders with each customer's name and city, biggest first.

Save yourself the pain

Common mistakes

Oops

Running $lookup on the whole collection and filtering afterwards.

Do this instead

Put $match before $lookup so you only join documents you actually need.

Oops

Designing every screen around $lookup, as if MongoDB were SQL.

Do this instead

Lookups are fine for reports. For hot screens, consider embedding a few fields to avoid joins.

Your mission

Join posts with authors

~25 min

Quick check

After $lookup with as: "customer", what type is the customer field?

Go deeper

  1. 1Join orders with users to show the customer name on each order.
  2. 2Use $unwind after $lookup when you expect exactly one match.
  3. 3If you need $lookup on every request, ask whether embedding would be simpler.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge