-3

嗨,我想寻求帮助,如果我怎样才能实现总数将乘以 0.20 并将被命名为交易。

这是我目前的聚合。 见游乐场

4

1 回答 1

2

为什么不只添加一个交易字段?

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"
          ]
        }
      }
    }
  }
})
于 2021-01-14T06:25:02.333 回答