【发布时间】:2018-10-06 06:07:35
【问题描述】:
我有两个集合,orders 和 products。我喜欢加入所有order.items[] 到products 集合,以向items[] 添加更多字段
示例数据:
订单[{ _id: 1, items: [
{ product_id: 1, price: 1.99, qty: 2 },
{ product_id: 2, price: 3.99, qty: 5 } ]}]
产品[{ _id: 1, name: "Product 1" }, { _id: 2, name: "Product 2 }]
预期输出:[{ _id: 1, items: [
{ product_id: 1, name: "Product 1", price: 1.99, qty: 2 },
{ product_id: 2, name: "Product 2",, price: 3.99, qty: 5 } ]}]
我尝试使用 $lookup 和管道 (mongodb 3.6) 并没有获取名称值,甚至匹配不起作用。
感谢您的帮助!
【问题讨论】:
标签: mongodb aggregation-framework