MongoDB 一次聚合中的两个 $multiply
MongoDB two $mulitply in one aggregation
您好,请教如何实现总和乘以0.20命名为交易
这是我当前的聚合。
See Playground
为什么不直接添加交易字段?
db.collection.aggregate({
$addFields: {
"products": {
$map: {
input: "$products",
as: "p",
in: {
"$mergeObjects": [
{
total: {
$multiply: [
"$$p.price",
"$$p.quantity"
]
},
transactions: {
$multiply: [
"$$p.price",
"$$p.quantity",
0.2
]
}
},
"$$p"
]
}
}
}
}
})
您好,请教如何实现总和乘以0.20命名为交易
这是我当前的聚合。 See Playground
为什么不直接添加交易字段?
db.collection.aggregate({
$addFields: {
"products": {
$map: {
input: "$products",
as: "p",
in: {
"$mergeObjects": [
{
total: {
$multiply: [
"$$p.price",
"$$p.quantity"
]
},
transactions: {
$multiply: [
"$$p.price",
"$$p.quantity",
0.2
]
}
},
"$$p"
]
}
}
}
}
})