Complete the code to perform a basic $lookup to join 'orders' with 'customers' on 'customerId'.
db.orders.aggregate([{ $lookup: { from: 'customers', localField: 'customerId', foreignField: '[1]', as: 'customerInfo' } }])The localField in 'orders' matches the foreignField in 'customers'. Both are 'customerId' to join correctly.
Complete the $lookup stage to use a pipeline that matches 'orders' with 'products' by 'productId'.
db.orders.aggregate([{ $lookup: { from: 'products', let: { pid: '$productId' }, pipeline: [ { $match: { $expr: { $eq: ['$_id', '[1]'] } } } ], as: 'productDetails' } }])Inside the pipeline, $$pid refers to the variable defined in let which holds the 'productId' from 'orders'.
Fix the error in the $lookup pipeline to correctly filter 'reviews' for each 'product' by matching 'productId'.
db.products.aggregate([{ $lookup: { from: 'reviews', let: { prodId: '$_id' }, pipeline: [ { $match: { $expr: { $eq: ['$productId', '[1]'] } } } ], as: 'productReviews' } }])Inside the pipeline, variables from let must be referenced with $$. So $$prodId is correct.
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.
db.orders.aggregate([{ $lookup: { from: 'items', let: { oid: '$itemId' }, pipeline: [ { $match: { $expr: { $and: [ { $eq: ['[1]', '[2]'] }, { $gt: ['$quantity', 5] } ] } } } ], as: 'matchedItems' } }])The first blank is the field in 'items' collection, which is $_id (assuming item id is _id). The second blank is the variable from let, which is $$oid.
Fill all three blanks to create a $lookup pipeline that joins 'users' with 'orders' where 'userId' matches and only includes orders with status 'shipped'.
db.users.aggregate([{ $lookup: { from: 'orders', let: { uid: '$_id' }, pipeline: [ { $match: { $expr: { $eq: ['[1]', '[2]'] } } }, { $match: { status: [3] } } ], as: 'shippedOrders' } }])The $eq compares the 'orders.userId' field with the variable $$uid. The status filter matches the string 'shipped'.