【发布时间】:2021-10-01 20:32:16
【问题描述】:
我正在尝试使用 mongodb 聚合框架进行计算。所以我需要在另一种情况下使用投影项目(投影阶段的计算字段),但我无法得到结果。
这是我尝试的代码。
"$project": {
"currency": {"$ifNull": ["$voucher_foreign_amount.symbol", "NOT ENTERED" ]},
"amount": {"$cond": [{"$not": ["$voucher_foreign_amount"]}, "$voucher_amount", "$voucher_foreign_amount.amount"]},
"voucher_type": 1,
"voucher_payment_type": 1,
"voucher_foreign_amount": 1,
"station": 1,
"paid": {"$cond": [{"$eq": ["$voucher_type", "Paid"]}, "$amount", 1]}
}
在这个预测中,我无法获得付费字段,在我看来,这是因为最后一个条件不识别“$amount”。
那么,如何在另一个字段生成中使用金额字段?
【问题讨论】:
-
$amount只有在项目结束后才存在,解决方法可以是添加一个$set阶段只是在项目上方设置amount,或者保留1个项目并计算2次(第二个看起来更快) -
@Takis_ 谢谢,两个项目解决了我的问题,如果你回答我会接受。
标签: mongodb