我有两个集合,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和pipeline(mongodb 3.6)并且没有获取名称值,甚至匹配也无效。
感谢您的帮助!
答案 0 :(得分:1)
此查询将对您有所帮助,对不起,如果我没有使用v3.6。
glue
我将解释它们分为4个阶段:
我很确定有更清晰,更简单的方法来写这个,但这应该没有问题。