【问题标题】:MongoDB two $mulitply in one aggregationMongoDB 两个 $multiply 在一个聚合中
【发布时间】:2021-04-19 04:36:36
【问题描述】:

您好,我想寻求帮助,如果我怎样才能使总数乘以 0.20 并将被命名为事务。

这是我当前的聚合。 See Playground

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework mongodb-charts


    【解决方案1】:

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

    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"
              ]
            }
          }
        }
      }
    })
    

    【讨论】:

    • 谢谢!不知道我可以乘以 3 个值。
    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2021-09-05
    • 2020-06-13
    相关资源
    最近更新 更多