aggregation framework - Mongodb aggregate query improvement -
i have mysql query this, select total_entries of current , previous range, group id_station , area_type.
select id_station, time_begin, if(`date` >= '2017-02-16' , `date` <= '2017-02-28' ,sum(total_entries) ,0) total_entries, if(`date` >= '2017-02-16' , `date` <= '2017-02-28' ,sum(woman) ,0) woman, if(`date` >= '2017-02-16' , `date` <= '2017-02-28' ,sum(man) ,0) man, if(`date` >= '2017-02-16' , `date` <= '2017-02-28' ,sum(couple) ,0) couple, if(`date` >= '2017-02-01' , `date` <= '2017-02-15' ,sum(total_entries) ,0) total_entries_prev, if(`date` >= '2017-02-01' , `date` <= '2017-02-15' ,sum(woman) ,0) total_woman_prev, if(`date` >= '2017-02-01' , `date` <= '2017-02-15' ,sum(man) ,0) total_man_prev, if(`date` >= '2017-02-01' , `date` <= '2017-02-15' ,sum(couple) ,0) total_couple_prev report_all_hour id_station in(493,494,495,496,517,518,960,961,962,963,964,965,1054,1499) , `date` >= '2017-02-02' , `date` <= '2017-02-28' , total_entries >0 , time_begin >="12:00:00" , time_begin < "15:00:00" group area_type, id_station rollup
in example used total_entries in real i'm using on 1000 columns. question how select on 1000 columns(girl, boy, couple, etc.) without add more condition in following coding:
$match = array( 'date' => array('$gte' => $this->mongotime("2017-02-01"),'$lte' => $this->mongotime("2017-02-31")), 'time_begin' => array('$gte' => 43200,'$lt' => 54000) ); $sort = array('$sort' => array('_id' => 1)); $cond_total_entries = array('$cond' => array( array( '$and' => array( array('$gte' => array('$date', $this->mongotime("2017-02-01"))),array('$lte' => array('$date', $this->mongotime("2017-02-15"))), ) ) , '$total_entries', 0 ) ); $cond_total_entries_prev = array('$cond' => array( array( '$and' => array( array('$gte' => array('$date', $this->mongotime("2017-02-16"))), array('$lte' => array('$date', $this->mongotime("2017-02-31"))), ) ) , '$total_entries', 0 ) ); $group_stores = array('_id' => array('area_type'=>'$area_type', 'id_station'=>'$id_station'), "total_entries_cur" => array('$sum' => $cond_total_entries), "total_entries_prev" => array('$sum' => $cond_total_entries_prev), ); $group_all = array('_id' => null, "total_entries_cur_sum" => array('$sum' => '$doc.total_entries_cur'), "total_entries_prev_sum" => array('$sum' => '$doc.total_entries_prev'), "data"=>array('$push' => '$doc') ); $table = $collection->aggregate([ ['$match' => $match], ['$group' => $group_stores], $sort, ['$project' => array('doc'=>array('_id'=>'$_id',"total_entries_cur"=>'$total_entries_cur',"total_entries_prev"=>'$total_entries_prev'))], ['$group' => $group_all] ]); $table = $table->toarray()[0]->getarraycopy(); $data = $table["data"]; echo "total entries current: ". $table["total_entries_cur_sum"] . "<br>"; echo "total entries previous: ". $table["total_entries_prev_sum"] . "<br>"; foreach ($data $item) { echo "area: ". $item['_id']["area_type"] . ", station: " . $item['_id']["id_station"] . ", total entries current: " . $item['total_entries_cur'] . ", total entries prev: " . $item['total_entries_prev'] . "<br>"; }
results:
total entries current: 974 total entries previous: 1263 area: 1, station: 493, total entries current: 254, total entries prev: 381 area: 1, station: 494, total entries current: 169, total entries prev: 166 area: 1, station: 495, total entries current: 60, total entries prev: 90 area: 1, station: 517, total entries current: 41, total entries prev: 57 area: 1, station: 960, total entries current: 70, total entries prev: 99 area: 1, station: 961, total entries current: 118, total entries prev: 162 area: 1, station: 962, total entries current: 1, total entries prev: 0 area: 1, station: 963, total entries current: 123, total entries prev: 119 area: 1, station: 965, total entries current: 97, total entries prev: 123 area: 1, station: 1499, total entries current: 41, total entries prev: 66
Comments
Post a Comment