我正在嘗試創建一個聚合,它將計算物件陣列中值的分布并回傳一個計算值陣列。
這是一個示例檔案
[
{
"duration": 1208,
"dataPoints": 2,
"binMin": 0,
"binMax": 5000
},
{
"duration": 25735,
"dataPoints": 3,
"binMin": 5000,
"binMax": 10000
},
{
"duration": 0,
"dataPoints": 0,
"binMin": 10000,
"binMax": 20000
},
{
"duration": 54088,
"dataPoints": 2,
"binMin": 20000,
"binMax": 28817
}
]
我需要將每個物件的持續時間相加,然后計算每個物件的分布并回傳一個新陣列,如下所示:
[
{
"duration": 1208,
"dataPoints": 2,
"binMin": 0,
"binMax": 5000,
"ratio": 0.014907874763979
},
{
"duration": 25735,
"dataPoints": 3,
"binMin": 5000,
"binMax": 10000,
"ratio": 0.317594500870037
},
{
"duration": 0,
"dataPoints": 0,
"binMin": 10000,
"binMax": 20000,
"ratio": 0
},
{
"duration": 54088,
"dataPoints": 2,
"binMin": 20000,
"binMax": 28817,
"ratio": 0.667497624365983
}
]
我能夠計算總持續時間并除以得到比率值,但它似乎只對陣列的第一個元素進行。
到目前為止,這是我的聚合:
[{$project: {
_id: '$_id',
username: 1,
uuid: 1,
data: '$stats.dataHistogram'
}}, {$unwind: {
path: '$data'
}}, {$group: {
_id: '$_id',
data_bin: {
$first: '$data'
},
total_duration: {
$sum: '$data.duration'
}
}}, {$project: {
_id: '$_id',
total_duration: 1,
data_bin: 1,
ratio: {
$divide: [
'$data_bin.duration',
{
$add: [
'$total_duration',
1
]
}
]
}
}}]
(我將 1 添加到 $total_duration 因為它有時可能是 0 并且我得到“不能被零除”錯誤)
我覺得我非常接近,但不確定下一步應該是什么。謝謝您的幫助!
uj5u.com熱心網友回復:
您可以使用$reduce先計算總持續時間。然后$divide通過使用明智地應用元素$map
db.collection.aggregate([
{
"$addFields": {
"totalDuration": {
"$reduce": {
"input": "$stats.histogram",
"initialValue": 0,
"in": {
$add: [
"$$value",
"$$this.duration"
]
}
}
}
}
},
{
"$addFields": {
"totalDuration": {
"$cond": {
"if": {
$eq: [
"$totalDuration",
0
]
},
"then": 1,
"else": "$totalDuration"
}
}
}
},
{
"$addFields": {
"stats.histogram": {
"$map": {
"input": "$stats.histogram",
"as": "h",
"in": {
"duration": "$$h.duration",
"dataPoints": "$$h.dataPoints",
"binMin": "$$h.binMin",
"binMax": "$$h.binMax",
"ratio": {
"$divide": [
"$$h.duration",
"$totalDuration"
]
}
}
}
}
}
}
])
這是Mongo游樂場供您參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/406701.html
標籤:
