【问题标题】:How to group data using mongodb?如何使用 mongodb 对数据进行分组?
【发布时间】:2015-03-18 09:48:46
【问题描述】:

我有以下数据,间隔为 15 分钟。

[{
    "_id" : ObjectId("5500a5e6f37a84d0509526ba"),
    "runtimeMilliSeconds" : NumberLong("1426105802063"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 71.72000122070312,
        "currentMemoryUtilization" : 77.4000015258789
    }
}
{
    "_id" : ObjectId("5500a96af37a84d0509526f8"),
    "runtimeMilliSeconds" : NumberLong("1426106701622"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 70.30000305175781,
        "currentMemoryUtilization" : 77.4000015258789
    }
}
{
    "_id" : ObjectId("5500aceef37a84d050952739"),
    "runtimeMilliSeconds" : NumberLong("1426107601441"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 73.2300033569336,
        "currentMemoryUtilization" : 77.4000015258789
    }
}
{
    "_id" : ObjectId("5500b07ff37a84d050952776"),
    "runtimeMilliSeconds" : NumberLong("1426108501342"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 60.61000061035156,
        "currentMemoryUtilization" : 77.4000015258789
    }
}


{
    "_id" : ObjectId("5500b404f37a84d0509527b7"),
    "runtimeMilliSeconds" : NumberLong("1426109402199"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 60.060001373291016,
        "currentMemoryUtilization" : 77.41000366210938
    }
}
{
    "_id" : ObjectId("5500b788f25a6f9765950f65"),
    "runtimeMilliSeconds" : NumberLong("1426110301345"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 58.689998626708984,
        "currentMemoryUtilization" : 77.41000366210938
    }
}
{
    "_id" : ObjectId("5500bb0cf37a84d050952837"),
    "runtimeMilliSeconds" : NumberLong("1426111202063"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 70.69999694824219,
        "currentMemoryUtilization" : 77.41000366210938
    }
}
{
    "_id" : ObjectId("5500be83f25a6f9765950fde"),
    "runtimeMilliSeconds" : NumberLong("1426112101980"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 69.41000366210938,
        "currentMemoryUtilization" : 77.44000244140625
    }
}

{
    "_id" : ObjectId("5500c206f37a84d0509528ac"),
    "runtimeMilliSeconds" : NumberLong("1426113001781"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 70.63999938964844,
        "currentMemoryUtilization" : 77.44000244140625
    }
}
{
    "_id" : ObjectId("5500c58cf37a84d0509528ea"),
    "runtimeMilliSeconds" : NumberLong("1426113901510"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 68.38999938964844,
        "currentMemoryUtilization" : 77.44000244140625
    }
}
{
    "_id" : ObjectId("5500c911f25a6f97659510a0"),
    "runtimeMilliSeconds" : NumberLong("1426114801403"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 77.7300033569336,
        "currentMemoryUtilization" : 77.44999694824219
    }
}
{
    "_id" : ObjectId("5500cca0f37a84d050952968"),
    "runtimeMilliSeconds" : NumberLong("1426115702206"),
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 74.23999786376953,
        "currentMemoryUtilization" : 77.4800033569336
    }
}]

我想按每小时间隔对这些数据进行分组。这意味着我想将每小时的 4 个文档分组到单个文档中,这样 'cpuMemoryStats' 键中的值将是所有四个的平均值。 runtimeMilliSeconds 也是 4 个文档的平均值。

即我想要第一到第四,第五到第八个文档。 我想要 12 个以上文档中的 4 个文档,平均键数。

示例输出为:

[{
    "_id" : ObjectId("5500a5e6f37a84d0509526ba"),
    "runtimeMilliSeconds" : 1426107152000,
    "cpuMemoryStats" : {
        "currentCpuUtilization" : 68.96500206,
        "currentMemoryUtilization" : 77.400001526
    }
}
.
.
..
]

我尝试了以下:

db.collection.aggregate({"$match": { "hostId" : "1.1.1.1" , "customerId"   : "customerId" ,
"runtimeMilliSeconds" : { "$gte" : 1426104902206}}},

{"$group" : {"_id" : { "$subtract" :[ {"$divide" : ["$runtimeMilliSeconds", 3600 ]},

{ "$mod" : [{"$divide" : ["$runtimeMilliSeconds", 3600 ]},1] } ] },

"memoryUtilization":{"$avg":"$cpuMemoryStats.currentMemoryUtilization"},
  "runtime":{"$avg":"$runtimeMilliSeconds"}}})

如何使用 mongo 按小时对数据进行分组???

【问题讨论】:

  • 几乎只要问题存在,答案就一直悬在那里。 _id 分组键的公式你的数学是错误的。

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

日期数学似乎是您存储格式的明确案例:

 db.collection.aggregate([
     { "$match": { 
         "hostId" : "1.1.1.1" , 
         "customerId" : "customerId" ,
         "runtimeMilliSeconds" : { "$gte" : 1426104902206 },
     }},
     { "$group" : {
         "_id" : { 
             "$subtract": [
                  "$runtimemilliSeconds",
                  { "$mod": [
                      "$runtimemilliSeconds",
                      1000 * 60 * 15 // 1000 ms x 60 sec * 15 mins     
                  ]}
             ]
         },
         "memoryUtilization": { "$avg": "$cpuMemoryStats.currentMemoryUtilization" },
         "runtime":{ "$avg": "$runtimeMilliSeconds" }
     }}
])

因此,作为记录,除了您要查找的一般结构之外,如图所示,900000 的正确“常数”是:

 1000 milliseconds
 x 60 seconds
 x 15 minutes

为了真正达到一个小时的间隔,您只需更改数字

 1000 milliseconds
 x 60 seconds
 x 60 minutes

这是一小时。所有的间隔都是这样完成的。但它是模数而不是除法。

【讨论】:

    【解决方案2】:

    我已经很接近回答了。我纠正了我的逻辑(数学)。这是正确的查询-

    db.collection.aggregate({
        "$match": {
        "hostId": "1.1.1.1",
        "customerId": "customerId",
        "runtimeMilliSeconds": {
            "$gte": 1426104902206
        }
        }
    },
    {
        "$group": {
        "_id": {
            "$subtract": [
                {
                    "$divide": [
                        "$runtimeMilliSeconds",
                        3600*1000
                    ]
                },
                {
                    "$mod": [
                        {
                            "$divide": [
                                "$runtimeMilliSeconds",
                                3600*1000
                            ]
                        },
                        1
                    ]
                }
            ]
        },
        "memoryUtilization": {
            "$avg": "$cpuMemoryStats.currentMemoryUtilization"
        },
        "runtime": {
            "$last": "$runtimeMilliSeconds"
        }
        }
    },
    {
        $sort: {
        runtime: 1
        }
    })
    

    此查询将每小时对所有数据进行分组,例如 8.00 到 9.00、9.00 到 10.00 等

    【讨论】:

      猜你喜欢
      • 2019-09-25
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多