0
0
MongoDBquery~10 mins

$lookup for joining collections 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 $lookup that joins the 'orders' collection with the 'customers' collection on the 'customerId' field.

MongoDB
db.orders.aggregate([{ $lookup: { from: "[1]", localField: "customerId", foreignField: "_id", as: "customerInfo" } }])
Drag options to blanks, or click blank then click option'
Acustomers
Bproducts
Csales
Demployees
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong collection name in the 'from' field.
Confusing 'from' with 'localField' or 'foreignField'.
2fill in blank
medium

Complete the code to join 'orders' with 'products' collection using $lookup on 'productId' and '_id'.

MongoDB
db.orders.aggregate([{ $lookup: { from: "products", localField: "[1]", foreignField: "_id", as: "productDetails" } }])
Drag options to blanks, or click blank then click option'
AcustomerId
BorderId
CproductId
DsupplierId
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'customerId' instead of 'productId' for product join.
Mixing up localField and foreignField.
3fill in blank
hard

Fix the error in the $lookup stage to correctly join 'orders' with 'suppliers' on 'supplierId'.

MongoDB
db.orders.aggregate([{ $lookup: { from: "suppliers", localField: "supplierId", foreignField: "[1]", as: "supplierInfo" } }])
Drag options to blanks, or click blank then click option'
AsupplierId
BsupplierID
Cid
D_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'supplierId' as foreignField instead of '_id'.
Incorrect casing like 'supplierID'.
4fill in blank
hard

Fill both blanks to join 'orders' with 'employees' collection on 'employeeId' and project only 'orderId' and 'employeeName'.

MongoDB
db.orders.aggregate([ { $lookup: { from: "[1]", localField: "employeeId", foreignField: "_id", as: "employeeInfo" } }, { $project: { orderId: 1, [2]: "$employeeInfo.name" } } ])
Drag options to blanks, or click blank then click option'
Aemployees
BcustomerName
CemployeeName
Dorders
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong collection name in 'from'.
Projecting with incorrect field names.
5fill in blank
hard

Fill all three blanks to join 'orders' with 'categories' on 'categoryId', unwind the joined array, and filter categories with name 'Beverages'.

MongoDB
db.orders.aggregate([ { $lookup: { from: "[1]", localField: "categoryId", foreignField: "_id", as: "categoryInfo" } }, { $unwind: "[2]" }, { $match: { "categoryInfo.[3]": "Beverages" } } ])
Drag options to blanks, or click blank then click option'
Acategories
B$categoryInfo
Cname
Dproducts
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong collection name in 'from'.
Incorrect unwind path without '$'.
Filtering on wrong field name.