Bird
Raised Fist0
MongoDBquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the $lookup stage do in a MongoDB aggregation pipeline?
easy
A. It deletes documents from a collection.
B. It joins documents from two collections based on a matching field.
C. It sorts documents in ascending order.
D. It updates documents with new values.

Solution

  1. Step 1: Understand the purpose of $lookup

    $lookup is used to combine documents from two collections by matching fields.
  2. Step 2: Compare with other options

    Deleting, sorting, or updating documents are different operations and not related to $lookup.
  3. Final Answer:

    It joins documents from two collections based on a matching field. -> Option B
  4. Quick Check:

    $lookup joins collections = A [OK]
Hint: Remember: $lookup is like a join in SQL [OK]
Common Mistakes:
  • Confusing $lookup with update or delete operations
  • Thinking $lookup sorts documents
  • Assuming $lookup modifies documents instead of joining
2. Which of the following is the correct syntax for a basic $lookup stage in MongoDB?
easy
A. { $lookup: { from: 'other', localField: 'id', foreignField: 'userId', joinAs: 'joinedData' } }
B. { $lookup: { collection: 'other', localKey: 'id', foreignKey: 'userId', output: 'joinedData' } }
C. { $lookup: { from: 'other', localField: 'id', foreignField: 'userId', as: 'joinedData' } }
D. { $lookup: { from: 'other', localField: 'id', foreignField: 'userId' } }

Solution

  1. Step 1: Identify correct field names in $lookup

    The correct keys are from, localField, foreignField, and as.
  2. Step 2: Check each option

    { $lookup: { from: 'other', localField: 'id', foreignField: 'userId', as: 'joinedData' } } uses all correct keys and syntax. Options A, B, and C have incorrect or missing keys.
  3. Final Answer:

    { $lookup: { from: 'other', localField: 'id', foreignField: 'userId', as: 'joinedData' } } -> Option C
  4. Quick Check:

    Correct $lookup keys = D [OK]
Hint: Use keys: from, localField, foreignField, as [OK]
Common Mistakes:
  • Using wrong key names like collection or joinAs
  • Omitting the 'as' field for output array
  • Missing foreignField or localField keys
3. Given two collections: orders with documents { _id: 1, productId: 101 } and products with documents { _id: 101, name: 'Pen' }, what will be the result of this aggregation on orders?
{ $lookup: { from: 'products', localField: 'productId', foreignField: '_id', as: 'productInfo' } }
medium
A. [{ _id: 1, productId: 101, productInfo: [{ _id: 101, name: 'Pen' }] }]
B. [{ _id: 1, productId: 101, productInfo: [] }]
C. [{ _id: 1, productId: 101 }]
D. SyntaxError

Solution

  1. Step 1: Match localField and foreignField values

    The productId in orders is 101, which matches _id 101 in products.
  2. Step 2: Understand $lookup output

    The matching product document is added as an array in productInfo.
  3. Final Answer:

    [{ _id: 1, productId: 101, productInfo: [{ _id: 101, name: 'Pen' }] }] -> Option A
  4. Quick Check:

    Matching product added in array = B [OK]
Hint: Matched docs appear in array named by 'as' [OK]
Common Mistakes:
  • Expecting productInfo to be a single object, not an array
  • Thinking no match means error instead of empty array
  • Ignoring the 'as' field name in output
4. What is wrong with this $lookup stage?
{ $lookup: { from: 'users', localField: 'userId', foreignField: 'id', as: 'userDetails' } }
Assuming the users collection uses _id as the user ID field.
medium
A. The 'as' field cannot be 'userDetails'.
B. The localField should be 'id' instead of 'userId'.
C. The 'from' field should be 'user' not 'users'.
D. The foreignField should be '_id' instead of 'id'.

Solution

  1. Step 1: Check foreignField correctness

    The users collection uses _id as the ID field, so foreignField must be '_id'.
  2. Step 2: Verify other fields

    localField and as are valid; collection name 'users' is correct.
  3. Final Answer:

    The foreignField should be '_id' instead of 'id'. -> Option D
  4. Quick Check:

    foreignField must match actual field = C [OK]
Hint: Match foreignField to actual field name in other collection [OK]
Common Mistakes:
  • Using wrong foreignField name
  • Changing 'as' field unnecessarily
  • Mistaking collection name pluralization
5. You have two collections: students with { _id: 1, name: 'Alice' } and grades with multiple documents per student like { studentId: 1, score: 85 }. How do you write a $lookup to get each student with all their grades in an array called allGrades?
hard
A. { $lookup: { from: 'grades', localField: '_id', foreignField: 'studentId', as: 'allGrades' } }
B. { $lookup: { from: 'grades', localField: 'studentId', foreignField: '_id', as: 'allGrades' } }
C. { $lookup: { from: 'grades', localField: 'name', foreignField: 'studentId', as: 'allGrades' } }
D. { $lookup: { from: 'grades', localField: '_id', foreignField: 'score', as: 'allGrades' } }

Solution

  1. Step 1: Identify matching fields between collections

    students._id matches grades.studentId, so these are the fields to join on.
  2. Step 2: Write $lookup with correct fields and output array name

    Use from: 'grades', localField: '_id', foreignField: 'studentId', and as: 'allGrades'.
  3. Final Answer:

    { $lookup: { from: 'grades', localField: '_id', foreignField: 'studentId', as: 'allGrades' } } -> Option A
  4. Quick Check:

    Match student _id to grades studentId = A [OK]
Hint: Join on matching ID fields, output array named by 'as' [OK]
Common Mistakes:
  • Swapping localField and foreignField
  • Using wrong fields like name or score for join
  • Incorrect output array name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard