Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
// Join orders with the users collection db.orders.aggregate([ { $match: { status: "shipped" } }, // Standard $lookup (foreign key join) { $lookup: { from: "users", localField: "userId", foreignField: "_id", as: "customer" }}, // $unwind turns the array into a single embedded doc { $unwind: { path: "$customer", preserveNullAndEmptyArrays: true } }, { $project: { orderId: "$_id", customerName: "$customer.name", customerEmail: "$customer.email", total: 1, status: 1 }}, // Pipeline $lookup with let — advanced join with filter // (shown as a second example) // { $lookup: { // from: "products", // let: { itemIds: "$items.productId" }, // pipeline: [ // { $match: { $expr: { $in: ["$_id", "$$itemIds"] } } }, // { $project: { name: 1, price: 1 } } // ], // as: "productDetails" // }} ])
Result
Open