简体   繁体   English

Mongodb在PHP中每天获得平均值

[英]Mongodb get average value over period of time per day in PHP

I am playing around with MongoDB and although I've seen posts on the internet about problems similar to mine, but I can't found out how to do it. 我正在玩MongoDB,虽然我在互联网上看过与我类似的问题,但我不知道该怎么做。

I have a quite some documents in my collection that hold sensor measurement data like this: 我的收藏中有一些文件包含传感器测量数据,如下所示:

{
      'timestamp' => new MongoDate(1503119239, 0),
      'sensor_id' => new MongoInt64(4),
      'value' => 12345
}

With of course the timestamp, sensor_id and value different every record. 当然,每个记录的时间戳,sensor_id和值都不同。

Using PHP I want to get the average values PER sensor PER day/month/hour. 使用PHP我想获得PER传感器PER日/月/小时的平均值。 For example I have tried getting the average per day by using 例如,我试过通过使用获得平均每天

$pipeline = array(
    array(
        '$group' => array(
            '_id'      => ['sensor_id'=> '$sensor', '$dayOfYear'=>'$timestamp'],
            'avgValue' => array( '$avg' => '$value' ),
        ),
    ),
);

$out = $logdataCollection->aggregate( $pipeline );

I'd expect it to give me an output of, for example 例如,我希望它能给我一个输出

[0] => ['sensor_id' => '1', 'dayOfYear'=>12, 'avgValue'=>1.123],
[1] => ['sensor_id' => '1', 'dayOfYear'=>13, 'avgValue'=>0.15],
[2] => ['sensor_id' => '2', 'dayOfYear'=>12, 'avgValue'=>1.123],
[3] => ['sensor_id' => '3', 'dayOfYear'=>15, 'avgValue'=>1.123],

etc. 等等

This example shows the average by day (over the period of a year), but I want to be able to this per hour as well over the period of a month: 此示例显示了按天计算的平均值(在一年中),但我希望能够在一个月内每小时执行此操作:

[0] => ['sensor_id' => '1', 'date'=>2015-01-01, 'avgValue'=>1.123],
[1] => ['sensor_id' => '1', 'date'=>2015-01-02, 'avgValue'=>0.15],
[2] => ['sensor_id' => '2', 'date'=>2015-01-01, 'avgValue'=>1.123],
[3] => ['sensor_id' => '3', 'date'=>2015-01-02, 'avgValue'=>1.123],

etc. 等等

At first my timestamps where just numeric but I thought inserting them as MongoDateTime would perhaps make a difference in this. 起初我的时间戳只是数字,但我认为将它们插入MongoDateTime可能会对此产生影响。 It didn't. 它没有。

I have read the docs about Aggregration framework, SO and the PHP website, but I just can not find out how to do it. 我已经阅读了关于Aggregration框架,SO和PHP网站的文档,但我无法找到如何做到这一点。 Please help! 请帮忙!

Finally I found out how to get the results for my examples. 最后,我发现了如何获得我的示例的结果。 I am groupping them on a formatted date as follows: 我正在按照以下格式化日期:

 $pipeline = array(
    array(
        '$group' => array(
            '_id'      => ['foundDate'=>['$dateToString'=>['format'=>'%Y-%m-%d', 'date'=>'$timestamp']], 'sensor'=>'$sensor'],
            'avgValue' => array( '$avg' => '$value' ),
        ),
    ),
);

In which i can set the date format to anything, the query will group on that. 我可以在其中将日期格式设置为任何内容,查询将对其进行分组。 For example the mentioned code will output: 例如,提到的代码将输出:

[0]=>
    array(2) {
      ["_id"]=>
      array(2) {
        ["foundDate"]=>
        string(10) "2016-03-23"
        ["sensor"]=>
        int(2)
      }
      ["avgValue"]=>
      float(526.622580645)
    }
    [1]=>
    array(2) {
      ["_id"]=>
      array(2) {
        ["foundDate"]=>
        string(10) "2016-03-23"
        ["sensor"]=>
        int(3)
      }
      ["avgValue"]=>
      float(480.773846154)
    }
   ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM