MongoDB $lookup Joins

Perform left outer joins between collections in aggregation pipelines.

The $lookup aggregation stage performs a left outer join to an unsharded collection in the same database to filter in documents from the "joined" collection for processing.

Key Takeaways: MongoDB $lookup Joins

Mastering MongoDB $lookup Joins helps you write clean and reliable MongoDB code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

MongoDB $lookup Joins Code Example

MongoDB
db.orders.aggregate([
  {
    $lookup: {
      from: "customers",
      localField: "customerId",
      foreignField: "_id",
      as: "customerDetails"
    }
  }
]);
Output
Returns orders with nested customerDetails array
Run this example on your own computer. The "Get Started" lesson shows how to set it up.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services