【发布时间】:2020-10-11 18:38:42
【问题描述】:
我有一个购物车,我正在计算总成本,但我尝试的所有方法似乎都不起作用:
示例篮子集合:
{
"hash": "xxxxx",
"items": [
{
productCode: 'xxx',
qty: 4
}
]
}
示例产品集合:
{
[
{
productCode: 'xxx',
price: 299
}
]
}
我当前的代码:
const basket = await this.collection.aggregate([
{ $match: { hash } }, // Find the shopping cart with the hash
{ $lookup: { from: 'products', localField: 'items.productCode', foreignField: 'productCode', as: 'products' } },
{ $limit: 1 },
{ $project: {
_id: false,
qtys: '$items',
products: '$products'
// totalCost // Output the total cost of all the products
}
}
]).toArray();
我需要通过将价格乘以商品数据中的数量来计算价格...有什么想法吗?
谢谢
【问题讨论】:
-
$multiply的 MongoDB 文档示例似乎正是您想要做的:docs.mongodb.com/manual/reference/operator/aggregation/multiply/…
标签: node.js mongodb e-commerce