我將以下檔案存盤在 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]
}
}
}
}
])
- 請幫助將計數添加到此查詢中?
- 如何將執行時間截斷到小數點后兩位?
- 如果對此查詢進行任何優化,請提出建議。
檔案:
[
{
"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")
}
]
蒙戈游樂場
uj5u.com熱心網友回復:
您的聚合實際上是在正確的軌道上。對于您的 3 個問題:
請幫助將計數添加到此查詢中?
只需使用將計數分解為 3 個條件計數 $cond
如何將執行時間截斷到小數點后兩位?
使用$round
如果對此查詢進行任何優化,請提出建議。我做了一個小調整,以分鐘為單位預先計算持續時間,而不是在
$group舞臺上再次計算它們
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 游樂場供您參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/359644.html
上一篇:聚合函式不添加新列MongoDB
