What does the $lookup stage do in a MongoDB aggregation pipeline?
It performs a left outer join to another collection in the same database, combining documents from both collections based on a matching field.
Click to reveal answer
beginner
Which fields are required in a basic $lookup stage?
You need from (the collection to join), localField (field from input documents), foreignField (field from the from collection), and as (name of the new array field to add).
Click to reveal answer
beginner
How does $lookup handle documents with no matching join field in the foreign collection?
It still includes the original document but adds an empty array in the as field, showing no matches were found.
Click to reveal answer
intermediate
Can $lookup join collections on fields with different names?
Yes, by specifying localField and foreignField with the respective field names, even if they differ.
Click to reveal answer
beginner
What is the output type of the joined data added by $lookup?
The joined data is added as an array field to each input document, containing all matching documents from the foreign collection.
Click to reveal answer
What does the as field specify in a $lookup stage?
AThe name of the new array field to store joined documents
BThe foreign collection name
CThe local field to match
DThe condition to filter documents
✗ Incorrect
The as field defines the name of the array field added to each document to hold the joined documents.
If no matching documents are found in the foreign collection, what does $lookup add?
AA null value
BAn empty array in the <code>as</code> field
CNo field is added
DAn error is thrown
✗ Incorrect
MongoDB adds an empty array in the as field to show no matches were found.
Which of these is NOT a required field in a basic $lookup?
Afrom
BlocalField
Cpipeline
DforeignField
✗ Incorrect
The pipeline field is optional and used for advanced joins; basic $lookup requires from, localField, and foreignField.
What type of join does $lookup perform?
AInner join
BRight outer join
CFull outer join
DLeft outer join
✗ Incorrect
$lookup performs a left outer join, including all documents from the input collection.
Can $lookup join collections from different databases?
ANo, only within the same database
BYes, always
CYes, but only with special permissions
DOnly if collections have the same name
✗ Incorrect
$lookup can only join collections within the same database.
Explain how the $lookup stage works in MongoDB aggregation pipelines.
Think about how two tables join in SQL but in MongoDB's document style.
You got /4 concepts.
Describe a scenario where you would use $lookup and what the result looks like.
Imagine combining customer info with their purchases.
You got /4 concepts.
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
Step 1: Understand the purpose of $lookup
$lookup is used to combine documents from two collections by matching fields.
Step 2: Compare with other options
Deleting, sorting, or updating documents are different operations and not related to $lookup.
Final Answer:
It joins documents from two collections based on a matching field. -> Option B
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?
The correct keys are from, localField, foreignField, and as.
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.
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?
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
Step 1: Check foreignField correctness
The users collection uses _id as the ID field, so foreignField must be '_id'.
Step 2: Verify other fields
localField and as are valid; collection name 'users' is correct.
Final Answer:
The foreignField should be '_id' instead of 'id'. -> Option D
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?