【发布时间】:2020-05-24 17:29:53
【问题描述】:
对于每个 product_id,我想要所有订单中的价格总和以及我想要实现的另一个结果是同一数组中具有相同 id 的产品的价格总和。
\\orders collection:
{
order_id: 123,
customer_id: 1,
order_items: [{product_id:1, price: 2}, {product: 4, price: 8}, {product_id:1, price: 2}]
},
{
order_id: 124,
customer_id: 5,
order_items: [{product_id:5, price: 7}, {product: 4, price: 8}]
}
我想要的第一个结果:
{product_id: 1, tot_price: 4},
{product_id: 4, tot_price: 16},
{product_id: 5, tot_price: 7}
第二个结果:
{order_id:123,
product_tot: [{product_id:1, tot_price: 4}, {product: 4, tot_price: 8}]},
{order_id:124,
product_tot: [{product_id:5, tot_price: 7}, {product: 4, tot_price: 8}},
【问题讨论】:
标签: mongodb group-by embedded-database