0
0
MongoDBquery~10 mins

$lookup with pipeline (advanced join) in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to perform a basic $lookup to join 'orders' with 'customers' on 'customerId'.

MongoDB
db.orders.aggregate([{ $lookup: { from: 'customers', localField: 'customerId', foreignField: '[1]', as: 'customerInfo' } }])
Drag options to blanks, or click blank then click option'
AcustomerId
BorderId
Cname
Daddress
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field that does not exist in 'customers' like 'orderId'.
Mixing up localField and foreignField names.
2fill in blank
medium

Complete the $lookup stage to use a pipeline that matches 'orders' with 'products' by 'productId'.

MongoDB
db.orders.aggregate([{ $lookup: { from: 'products', let: { pid: '$productId' }, pipeline: [ { $match: { $expr: { $eq: ['$_id', '[1]'] } } } ], as: 'productDetails' } }])
Drag options to blanks, or click blank then click option'
A$_id
B$productId
C$$pid
D$$productId
Attempts:
3 left
💡 Hint
Common Mistakes
Using single dollar sign like '$pid' instead of double '$$pid'.
Referencing the field directly without using the variable.
3fill in blank
hard

Fix the error in the $lookup pipeline to correctly filter 'reviews' for each 'product' by matching 'productId'.

MongoDB
db.products.aggregate([{ $lookup: { from: 'reviews', let: { prodId: '$_id' }, pipeline: [ { $match: { $expr: { $eq: ['$productId', '[1]'] } } } ], as: 'productReviews' } }])
Drag options to blanks, or click blank then click option'
A$prodId
B$$prodId
C$_id
D$$productId
Attempts:
3 left
💡 Hint
Common Mistakes
Using single dollar sign '$prodId' which is invalid.
Using the field name 'productId' directly instead of the variable.
4fill in blank
hard

Fill both blanks to create a $lookup pipeline that matches 'orders' with 'items' where 'itemId' equals the order's 'itemId' and filters items with quantity greater than 5.

MongoDB
db.orders.aggregate([{ $lookup: { from: 'items', let: { oid: '$itemId' }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['[1]', '[2]'] }, { $gt: ['$quantity', 5] } ] } } } ], as: 'matchedItems' } }])
Drag options to blanks, or click blank then click option'
A$$oid
B$itemId
C$$itemId
D$_id
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of the fields in $eq.
Using single dollar sign for the variable.
5fill in blank
hard

Fill all three blanks to create a $lookup pipeline that joins 'users' with 'orders' where 'userId' matches and only includes orders with status 'shipped'.

MongoDB
db.users.aggregate([{ $lookup: { from: 'orders', let: { uid: '$_id' }, pipeline: [ { $match: { $expr: { $eq: ['[1]', '[2]'] } } }, { $match: { status: [3] } } ], as: 'shippedOrders' } }])
Drag options to blanks, or click blank then click option'
A$userId
B$$uid
C'shipped'
D'delivered'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong field name in $eq.
Forgetting quotes around the status string.
Using single dollar sign for the variable.