Database
[{"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")}]
Query
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"}}}])