0
0
MongoDBquery~20 mins

$lookup with pipeline (advanced join) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $lookup Pipeline Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this $lookup with pipeline?

Given two collections orders and products, what will be the result of the following aggregation on orders?

{
  $lookup: {
    from: "products",
    let: { product_id: "$productId" },
    pipeline: [
      { $match: { $expr: { $eq: ["$_id", "$$product_id"] } } },
      { $project: { name: 1, price: 1, _id: 0 } }
    ],
    as: "productDetails"
  }
}

Assume orders has a document { _id: 1, productId: 101, quantity: 2 } and products has { _id: 101, name: "Pen", price: 1.5 }.

A[{ _id: 1, productId: 101, quantity: 2, productDetails: [{ name: "Pen", price: 1.5 }] }]
B[{ _id: 1, productId: 101, quantity: 2, productDetails: [{ _id: 101, name: "Pen", price: 1.5 }] }]
C[{ _id: 1, productId: 101, quantity: 2, productDetails: [] }]
DSyntaxError: Invalid $lookup pipeline syntax
Attempts:
2 left
💡 Hint

Remember that $project can exclude _id by setting it to 0.

📝 Syntax
intermediate
2:00remaining
Which $lookup pipeline syntax is invalid?

Identify the option that will cause a syntax error in a MongoDB aggregation $lookup stage using a pipeline.

A{ $lookup: { from: "items", let: { id: "$itemId" }, pipeline: [ { $match: { $expr: { $eq: ["$_id", "$id"] } } } ], as: "details" } }
B} } "sliated" :sa ,] } } } ]"di$$" ,"di_$"[ :qe$ { :rpxe$ { :hctam$ { [ :enilepip ,} "dImeti$" :di { :tel ,"smeti" :morf { :pukool$ {
C{ $lookup: { from: "items", let: { id: "$itemId" }, pipeline: [ { $match: { $expr: { $eq: ["$_id", "$$id"] } } } ], as: "details" } }
D $lookup: { from: "items", let: { id: "$itemId" }, pipeline: [ { $match: { $expr: { $eq: ["$_id", "$$id"] } } } ], as: "details" } }
Attempts:
2 left
💡 Hint

Check how variables are referenced inside the pipeline with let.

optimization
advanced
2:00remaining
How to optimize $lookup pipeline for large collections?

You have a $lookup with a pipeline joining orders to products. The products collection is very large. Which option best improves performance?

AUse $group in the pipeline to aggregate all products before matching
BUse $unwind on the <code>products</code> collection before $lookup
CRemove the pipeline and use simple localField/foreignField matching
DAdd an index on <code>products._id</code> and use $match with $expr in the pipeline
Attempts:
2 left
💡 Hint

Indexes help queries run faster, especially on large collections.

🔧 Debug
advanced
2:00remaining
Why does this $lookup pipeline return empty arrays?

Given this aggregation on orders:

{
  $lookup: {
    from: "products",
    let: { pid: "$productId" },
    pipeline: [
      { $match: { $expr: { $eq: ["$_id", "$pid"] } } }
    ],
    as: "productInfo"
  }
}

It returns productInfo as empty arrays even though matching documents exist. What is the cause?

AThe <code>productId</code> field does not exist in <code>orders</code>
BVariable <code>pid</code> is referenced incorrectly without <code>$$</code> prefix inside $expr
CThe <code>from</code> collection name is misspelled
DThe <code>$match</code> stage is missing a $project
Attempts:
2 left
💡 Hint

Check how variables from let are used inside the pipeline.

🧠 Conceptual
expert
2:00remaining
What is the main advantage of using $lookup with a pipeline over localField/foreignField?

Choose the best explanation for why you would use $lookup with a pipeline instead of the simpler localField/foreignField syntax.

AIt automatically indexes the joined fields for better performance
BIt always runs faster than localField/foreignField joins
CIt allows complex filtering, transformations, and multiple stages on the joined collection before embedding results
DIt returns results as a flat list instead of an array
Attempts:
2 left
💡 Hint

Think about flexibility and control over the joined data.