Complete the code to perform a $lookup that joins the 'orders' collection with the 'customers' collection on the 'customerId' field.
db.orders.aggregate([{ $lookup: { from: "[1]", localField: "customerId", foreignField: "_id", as: "customerInfo" } }])The from field in $lookup specifies the collection to join with. Here, we join 'orders' with 'customers'.
Complete the code to join 'orders' with 'products' collection using $lookup on 'productId' and '_id'.
db.orders.aggregate([{ $lookup: { from: "products", localField: "[1]", foreignField: "_id", as: "productDetails" } }])The localField is the field in the 'orders' collection that matches the '_id' in 'products'. Here, it's 'productId'.
Fix the error in the $lookup stage to correctly join 'orders' with 'suppliers' on 'supplierId'.
db.orders.aggregate([{ $lookup: { from: "suppliers", localField: "supplierId", foreignField: "[1]", as: "supplierInfo" } }])The foreignField should be the field in 'suppliers' that matches 'supplierId' in 'orders'. Usually, this is '_id'.
Fill both blanks to join 'orders' with 'employees' collection on 'employeeId' and project only 'orderId' and 'employeeName'.
db.orders.aggregate([ { $lookup: { from: "[1]", localField: "employeeId", foreignField: "_id", as: "employeeInfo" } }, { $project: { orderId: 1, [2]: "$employeeInfo.name" } } ])The from collection is 'employees'. The projected field name for the employee's name is 'employeeName'.
Fill all three blanks to join 'orders' with 'categories' on 'categoryId', unwind the joined array, and filter categories with name 'Beverages'.
db.orders.aggregate([ { $lookup: { from: "[1]", localField: "categoryId", foreignField: "_id", as: "categoryInfo" } }, { $unwind: "[2]" }, { $match: { "categoryInfo.[3]": "Beverages" } } ])The from collection is 'categories'. The $unwind stage uses '$categoryInfo' to flatten the array. The $match filters by the 'name' field inside 'categoryInfo'.