【发布时间】:2016-06-08 13:43:04
【问题描述】:
嘿,我无法正确进行聚合。
我有这个数据集,并且在集合中还有几百万个其他类似的文档:
{
"_id": ObjectId("5757c73344ce54ae1d8b456c"),
"hostname": "Baklap4",
"timestamp": NumberLong(1465370500),
"networkList": [
{
"name": "46.243.152.13",
"openConnections": NumberLong(3)
},
{
"name": "46.243.152.50",
"openConnections": NumberLong(4)
}
],
"webserver": "nginx",
"deviceList": [
{
"deviceName": "eth0",
"receive": NumberLong(183263),
"transmit": NumberLong(781595)
},
{
"deviceName": "wlan0",
"receive": NumberLong(0),
"transmit": NumberLong(0)
}
]
}
我想要什么:
我想获得一个结果集,我在 300 秒的时间跨度内对每个文档进行平均(每个数值)。
[
[
'$match' => [
'timestamp' => ['$gte' => $todayMidnight],
'hostname' => $serverName
]
],
[
'$unwind' => '$networkList'
],
[
'$unwind' => '$deviceList'
],
[
'$group' => [
'_id' => [
'interval' => [
'$subtract' => [
'$timestamp',
[
'$mod' => ['$timestamp', 300]
]
]
],
'network' => '$networkList.name',
'device' => '$deviceList.name',
],
'openConnections' => [
'$sum' => '$networkList.openConnections'
],
'cpuLoad' => [
'$avg' => '$cpuLoad'
],
'bytesPerSecond' => [
'$avg' => '$bytesPerSecond'
],
'requestsPerSecond' => [
'$avg' => '$requestsPerSecond'
],
'webserver' => [
'$last' => '$webserver'
],
'timestamp' => [
'$max' => '$timestamp'
]
]
],
[
'$project' => [
'_id' => 0,
'timestamp' => 1,
'cpuLoad' => 1,
'bytesPerSecond' => 1,
'requestsPerSecond' => 1,
'webserver' => 1,
'openConnections' => 1,
'networkList' => '$networkList',
'deviceList' => '$_id.device',
]
],
[
'$sort' => [
'timestamp' => -1
]
]
];
但这并没有给我一个包含所有设备和每个设备平均接收和传输字节数的列表。
如何获得这些?
【问题讨论】:
-
您是从数据库中提取数据吗?如果是,您可以提交数据的整个结构吗? - 最好使用查询在数据库中进行聚合
-
10 分钟间隔对你有用吗?
-
@Miro 是的,我正在从 MongoDB 中提取数据。数据结构就像第一个数据集。
-
@profesor79 这也可以,我只需将 300 秒(5 分钟)更改为 600 秒。
标签: mongodb symfony aggregation-framework