【发布时间】:2020-11-01 12:21:29
【问题描述】:
我的数据库中有以下集合:
{
"username" : "John",
"shopping_cart" : {
"coffee" : 2,
"chocolate" : 3
(...will have about 50 different products that can be added to the shopping cart)
},
}
有没有一种方法可以汇总用户添加到购物车中的所有商品并获得购物车全部数量的总和?像这样的。
db.items.aggregate([
{$group: {
_id: null,
prices: {$sum: "$all_items_in_shopping_cart}
}}
])
【问题讨论】:
标签: mongodb