php - Group by mongodate in mongodb -


my insertd data

   {    "_id" : objectid("5992e6f52479d56b0b2709bf"),          "timestamp" : isodate("2017-08-15t12:19:25.680+0000"),          "record" : 20507361.0,          "battv_min" : 11.9,          "sevolt" : 0.0     } 

and query getting date is

        $collection1 = $db->tablename;         $filter1 = array('timestamp' => array('$gte' => $acst1, '$lte' => $acst2));         $options1 = array('projection' => array('_id' => 0,'temperature' => 1,timestamp => 1));         $oresult = $collection1->find($filter1,$options1); 

i getting data in billions want optimize query using group method, if have data below 70000 group second if have data range between 70000 100000 group minute on minute, time , last day.

you can use aggregate function use group in mongodb project date format want , if want column value use $addtoset function , project average value use avg function.

$collection1 = $db->tablename;                 $oresult = $collection1->aggregate([                                 [                                     '$match' => array(timestamp => array('$gte' => $acst1, '$lte' => $acst2))                                 ],                                 [                                     '$project' => [                                     'year'  => ['$year' =>'timestamp'],                                         'month' => ['$month' => '$timestamp'],                                         'day'   => ['$dayofmonth' => '$timestamp'],                                         'hour'  => ['$hour' => '$timestamp'],                                         'minute'   => ['$minute' => '$timestamp'],                                         'second'   => ['$second' => '$timestamp'],                                         'millisecond'   => ['$millisecond' => '$timestamp'],                                             'value'   => '$battv'                                      ]                                 ],                                 [                                     '$group' => [                                         '_id'    => [                                             'year'  => '$year',                                             'month' => '$month',                                             'day'   => '$day',                                             'hour'  => '$hour',                                             'minute'   => '$minute',                                             'second' => '$second',                                             'millisecond' => '$millisecond'                                          ],                                         'count'  => ['$sum' => 1],                                         'values' => ['$addtoset' => '$value']                                     ]                                 ],                                  [                                     '$sort' => ['_id' => 1]                                 ],                                 [                                     '$project' => [                                         'a' => '$count',                                         'new' =>  ['$avg' => '$values'],                                         'y' => [                                             '$concat' => [                                                 ['$substr' => ['$_id.year', 0, 4]],                                                 '-',                                                 [                                                     '$cond' => [                                                         ['$lte' => ['$_id.month', 9]],                                                         [                                                             '$concat' => [                                                                 '0',                                                                 ['$substr' => ['$_id.month', 0, 2]],                                                             ]                                                         ],                                                         ['$substr' => ['$_id.month', 0, 2]]                                                     ]                                                 ],                                                 '-',                                                 [                                                     '$cond' => [                                                         ['$lte' => ['$_id.day', 9]],                                                         [                                                             '$concat' => [                                                                 '0',                                                                 ['$substr' => ['$_id.day', 0, 2]],                                                             ]                                                         ],                                                         ['$substr' => ['$_id.day', 0, 2]]                                                     ]                                                 ],                                                 ' ',                                                 [                                                     '$cond' => [                                                         ['$lte' => ['$_id.hour', 9]],                                                         [                                                             '$concat' => [                                                                 '0',                                                                 ['$substr' => ['$_id.hour', 0, 2]],                                                             ]                                                         ],                                                         ['$substr' => ['$_id.hour', 0, 2]]                                                     ]                                                 ],                                                 ':',                                                 [                                                     '$cond' => [                                                         ['$lte' => ['$_id.minute', 9]],                                                         [                                                             '$concat' => [                                                                 '0',                                                                 ['$substr' => ['$_id.minute', 0, 2]],                                                             ]                                                         ],                                                         ['$substr' => ['$_id.minute', 0, 2]]                                                     ]                                                 ],                                                 ':',                                                 [                                                     '$cond' => [                                                         ['$lte' => ['$_id.second', 9]],                                                         [                                                             '$concat' => [                                                                 '0',                                                                 ['$substr' => ['$_id.second', 0, 2]],                                                             ]                                                         ],                                                         ['$substr' => ['$_id.second', 0, 2]]                                                     ]                                                 ],                                                 '.',                                                 [                                                     '$cond' => [                                                         ['$lte' => ['$_id.millisecond', 9]],                                                         [                                                             '$concat' => [                                                                 '0',                                                                 ['$substr' => ['$_id.millisecond', 0, 1]],                                                             ]                                                         ],                                                         ['$substr' => ['$_id.millisecond', 0, 1]]                                                     ]                                                 ]                                              ]                                         ],                                     ]                                 ]                             ]                         ); 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -