| 语法 | { $lookup: { from, localField, foreignField, as } } |
|---|---|
| 语义 | 左外连接:无匹配时 as 为空数组 |
| 性能 | foreignField 侧应有索引;结果集会随匹配数量膨胀 |
示例
示例
db.orders.aggregate([
{ $lookup: { from: "products", localField: "sku", foreignField: "sku", as: "product" } },
{ $unwind: "$product" }
])$unwind 紧跟 $lookup 可把单元素数组拍平成对象形式;多元素匹配时保留数组。