【问题标题】:Mongoose: $sum if conditionsMongoose: $sum if 条件
【发布时间】:2019-04-30 15:38:35
【问题描述】:

我有一个 query 与 find 和 aggregateSchedule 模型中找到,并计算服务总和和服务总价值总和。但我需要在“状态”等于 2 的条件下计算这个总和(totalServices 的总和)。我可以这样做吗?

我的查询:

 Schedule.find(findTerm)
    .skip(req.body.page * req.body.limit)
    .limit(Number(req.body.limit))
    .select(
      "service.name value scheduleStart scheduleEnd comissionValue status paymentMethod"
    )
    .exec((err, response) => {
      if (err) res.status(500).send(err);

      Schedule.find(findTerm)
        .count()
        .exec((error, count) => {
          if (error)
            res.status(500).send({
              error,
              code: 0,
              message: "Erro."
            });

          Schedule.aggregate([{
              $match: {              
                store: req.body.store,             
              }
            },
            {
              $group: {
                _id: {
                  id: "$store"
                },
                totalValue: {
                  $sum: "$value"
                },
                totalServices: {
                  $sum:  {
                    $cond: [ {
                      $eq: [ "$status", 2 ]
                    }]
                  }
                },
                count: {
                  $sum: 1
                }
              }
            }...

我的查询结果:

...{
            "service": {
                "name": "CABELO + BARBA"
            },
            "comissionValue": 0,
            "paymentMethod": 0,
            "_id": "5bfec336c6f00d2e88f8d765",
            "scheduleStart": "2018-11-28 14:35",
            "scheduleEnd": "2018-11-28 15:45",
            "status": 2,
            "value": 75
        },
        {
            "service": {
                "name": "Barba"
            },
            "comissionValue": 0,
            "paymentMethod": 0,
            "_id": "5bfec3ffc6f00d2e88f8d766",
            "scheduleStart": "2018-11-28 18:30",
            "scheduleEnd": "2018-11-28 18:50",
            "status": 2,
            "value": 20
        }
    ],
    "count": 4299,
    "group": [
        {
            "_id": {
                "id": "5b16cceb56a44e2f6cd0324b"
            },
            "totalValue": 777780048281, //right value
            "totalServices": 945, //wrong value
            "count": 676
        }
    ]
}

如果status 等于2,我需要将totalServices 的总和过滤为仅对象(我尝试使用$cond 但没有工作)。

【问题讨论】:

    标签: javascript mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    您需要有条件地检查status ($cond) 即如果status 等于($eq) 到2 然后$sum value 字段否则通过0

    Schedule.aggregate([
      { "$match": { "store": req.body.store }},
      { "$group": {
        "_id": { "id": "$store" },
        "totalValue": { "$sum": "$value" },
        "totalServices": {
          "$sum": { "$cond": [{ "$eq": ["$status", 2] }, 1, 0] }
        },
        "count": { "$sum": 1 }
      }}
    ])
    

    【讨论】:

    • totalServices 的结果。我需要计算所有具有 $status 等于 2 的对象
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2015-04-15
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    相关资源
    最近更新 更多