Joining Collections with $lookup

MongoDB $lookup – How to Do Joins

SQL-style joins in MongoDB

By Sharanmeet Singh
Tags:MongoDB$lookupJoins

MongoDB Query Generator

Generate MongoDB queries and aggregation pipelines

Try Generator

MongoDB $lookup

Joins in MongoDB are performed using $lookup.


Example

{
  "$lookup": {
    "from": "orders",
    "localField": "_id",
    "foreignField": "customerId",
    "as": "orders"
  }
}

This attaches all orders for each customer.


Tips

  • $lookup can be expensive on large collections
  • Use indexes on foreignField
  • Consider $unwind to flatten arrays

MongoDB is not a relational database—use joins sparingly.

MongoDB $lookup – How to Do Joins | QueryBoss