</>
Skip to content
MongoDB lessons (24/38)

MongoDB — Lookup

Basic Lookup

db.orders.aggregate([
  {
    $lookup: {
      from: "users",
      localField: "userId",
      foreignField: "_id",
      as: "user"
    }
  }
])

Parameters

ParameterDescription
fromCollection to join
localFieldField from input
foreignFieldField from joined
asOutput array field

Multiple Conditions

db.orders.aggregate([
  {
    $lookup: {
      from: "users",
      let: { userId: "$userId", status: "$status" },
      pipeline: [
        { $match: { $expr: { $eq: ["$_id", "$$userId"] } } }
      ],
      as: "user"
    }
  }
])

Unwind Result

db.orders.aggregate([
  { $lookup: { ... } },
  { $unwind: "$user" }
])

Mini Practice

  1. Perform basic lookup
  2. Use multiple conditions
  3. Unwind lookup results
  4. Combine with other stages

Up Next

Continue with Indexes — creating indexes.

Related Topics

Frequently Asked Questions about Lookup

What is Lookup in MongoDB?

Lookup is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Lookup?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Lookup.

Why is Lookup important in MongoDB?

Lookup is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.