0
0
MongoDBquery~20 mins

$lookup for joining collections in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB $lookup 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 aggregation?
Given two collections, orders and customers, what will be the result of this aggregation on orders?
MongoDB
db.orders.aggregate([
  {
    $lookup: {
      from: "customers",
      localField: "customerId",
      foreignField: "_id",
      as: "customer_info"
    }
  }
])
AThe aggregation will fail with a syntax error due to missing pipeline stages.
BEach customer document will have an array of orders under 'customer_info'.
CEach order document will have a new field 'customer_info' containing an array with matching customer documents.
DThe aggregation will return only orders that have no matching customer documents.
Attempts:
2 left
💡 Hint
Think about which collection the aggregation runs on and what $lookup does.
📝 Syntax
intermediate
2:00remaining
Which $lookup syntax is correct?
Identify the valid $lookup stage syntax to join 'products' with 'categories' on 'categoryId' and '_id'.
A{ $lookup: { from: 'categories', localField: 'categoryId', foreignField: '_id', as: 'category_info' } }
B{ $lookup: { from: 'categories', localField: 'categoryId', foreignField: '_id' } }
C{ $lookup: { from: 'categories', localField: 'categoryId', foreignField: 'categoryId', as: 'category_info' } }
D{ $lookup: { from: 'categories', localField: '_id', foreignField: 'categoryId', as: 'category_info' } }
Attempts:
2 left
💡 Hint
localField is from the current collection, foreignField is from the joined collection.
optimization
advanced
2:00remaining
How to optimize $lookup for large collections?
You have two large collections: 'sales' and 'products'. Which approach optimizes $lookup performance best?
AEnsure 'products._id' is indexed and use $lookup with localField and foreignField.
BUse $lookup with pipeline and $match inside it to filter 'products' before joining.
CLoad all 'products' into application memory and join manually.
DUse $lookup without indexes and rely on MongoDB to optimize automatically.
Attempts:
2 left
💡 Hint
Filtering early reduces data processed in join.
🔧 Debug
advanced
2:00remaining
Why does this $lookup return empty arrays?
Given these collections, why does this aggregation return empty arrays in 'joined_docs'?
MongoDB
db.collection1.aggregate([
  {
    $lookup: {
      from: "collection2",
      localField: "id",
      foreignField: "_id",
      as: "joined_docs"
    }
  }
])
AThe aggregation pipeline is missing a $match stage.
BThe 'from' collection name is misspelled.
CMongoDB does not support joining on '_id' fields.
DThe 'id' field in collection1 and '_id' in collection2 have different data types.
Attempts:
2 left
💡 Hint
Check if the fields used for joining have the same type.
🧠 Conceptual
expert
2:00remaining
What is the main limitation of $lookup in MongoDB?
Which statement best describes a key limitation of $lookup when joining collections?
A$lookup requires both collections to be in the same database and cannot join across databases.
B$lookup can only perform left outer joins and cannot do inner joins directly.
C$lookup automatically indexes foreign fields for faster joins.
D$lookup can join collections with different data types without issues.
Attempts:
2 left
💡 Hint
Think about database boundaries and $lookup capabilities.