Mongo 数据库查询计数条件

Mongo DB query for count with condition

我在 MongoDB 4.2 版的“部署”集合中存储了以下文档。

我想按 productId 分组并在一系列日期之间实现以下结果。

我已经使用这个查询实现了执行时间。

    db.getCollection('Deployments').aggregate([
{
    $match : {$and:[{ "startedAt": { $gte: new ISODate("2021-10-01") } }, 
                   { "startedAt": { $lte: new ISODate("2021-11-17") } }]}
  },
  {
      $group : {
          _id:"$productId",
          count: { $sum: 1 },
          minExecutionTime:
                    {
                        
                       $min:
                          {
                             $divide:[{$subtract:["$completedAt", "$startedAt"]}, 1000 * 60]
                                
                           }
                    },
          maxExecutionTime:
                    {
                       $max:
                          {
                             $divide:[{$subtract:["$completedAt", "$startedAt"]}, 1000 * 60]
                                
                           }
                    },
          avgExecutionTime:
                    {
                       $avg:
                          {
                             $divide:[{$subtract:["$completedAt", "$startedAt"]}, 1000 * 60]
                                
                           }
                    }
          }
      
      }
  ])
  1. 请帮忙将计数添加到此查询?
  2. 如何将执行时间截断到小数点后两位?
  3. 如果对此查询有任何优化,请提出建议。

文件:

[
  {
    "productId": 1,
    "deploymentStatus": "Succeeded",
    "startedAt": ISODate("2021-01-21T14:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T14:03:55.789Z")
  },
  {
    "productId": 2,
    "deploymentStatus": "Failed",
    "startedAt": ISODate("2021-01-21T15:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T15:03:55.789Z")
  },
  {
    "productId": 3,
    "deploymentStatus": "Cancelled",
    "startedAt": ISODate("2021-01-21T16:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T16:03:55.789Z")
  },
  {
    "productId": 1,
    "deploymentStatus": "Failed",
    "startedAt": ISODate("2021-01-21T17:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T17:03:55.789Z")
  },
  {
    "productId": 2,
    "deploymentStatus": "Failed",
    "startedAt": ISODate("2021-01-21T18:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T18:03:55.789Z")
  },
  {
    "productId": 3,
    "deploymentStatus": "Succeeded",
    "startedAt": ISODate("2021-01-21T19:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T19:03:55.789Z")
  },
  {
    "productId": 1,
    "deploymentStatus": "Cancelled",
    "startedAt": ISODate("2021-01-21T20:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T20:03:55.789Z")
  },
  {
    "productId": 2,
    "deploymentStatus": "Failed",
    "startedAt": ISODate("2021-01-21T21:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T21:03:55.789Z")
  },
  {
    "productId": 3,
    "deploymentStatus": "Succeeded",
    "startedAt": ISODate("2021-01-21T22:00:19.782Z"),
    "completedAt": ISODate("2021-01-21T22:03:55.789Z")
  }
]

Mongo Playground

你的聚合实际上是在正确的轨道上。对于您的 3 个问题:

Any help to add the counts to this query please?

只需使用 $cond

将计数分成 3 个条件计数

How to truncate the execution times to 2 decimal places?

使用$round

Please suggest in case of any optimizations to this query. I did a minor tweak to pre-compute the duration in minute instead of computing them again in the $group stage

db.collection.aggregate([
  {
    $match: {
      $and: [
        {
          "startedAt": {
            $gte: ISODate("2021-01-21")
          }
        },
        {
          "startedAt": {
            $lte: ISODate("2021-01-22")
          }
        }
      ]
    }
  },
  {
    "$addFields": {
      "durationInMin": {
        $round: [
          {
            $divide: [
              {
                $subtract: [
                  "$completedAt",
                  "$startedAt"
                ]
              },
              60000
            ]
          },
          2
        ]
      }
    }
  },
  {
    $group: {
      _id: "$productId",
      SucceedCount: {
        $sum: {
          "$cond": {
            "if": {
              $eq: [
                "$deploymentStatus",
                "Succeeded"
              ]
            },
            "then": 1,
            "else": 0
          }
        }
      },
      FailedCount: {
        $sum: {
          "$cond": {
            "if": {
              $eq: [
                "$deploymentStatus",
                "Failed"
              ]
            },
            "then": 1,
            "else": 0
          }
        }
      },
      CancelledCount: {
        $sum: {
          "$cond": {
            "if": {
              $eq: [
                "$deploymentStatus",
                "Cancelled"
              ]
            },
            "then": 1,
            "else": 0
          }
        }
      },
      minExecutionTime: {
        $min: "$durationInMin"
      },
      maxExecutionTime: {
        $max: "$durationInMin"
      },
      avgExecutionTime: {
        $avg: "$durationInMin"
      }
    }
  }
])

这里是Mongo playground供您参考。