$lookup performs a left outer join from the current collection to another collection in the same database, attaching matched documents as an array field on each input document. It is the aggregation pipeline's equivalent of SQL's LEFT OUTER JOIN.
| SQL | MongoDB $lookup |
|---|---|
LEFT OUTER JOIN orders ON users._id = orders.userId | $lookup: { from:"orders", localField:"_id", foreignField:"userId", as:"orders" } |
| Inline columns from joined row | Joined docs added as an array field |
| NULL row if no match | Empty array [] if no match |
$lookup is always an array, regardless of how many documents match. Zero matches = []. One match = [{ ... }]. Follow with $unwind + preserveNullAndEmptyArrays: true to flatten a one-to-one join into an embedded object.