我想将聚合用于组,但如何使用laravel-mongodb:
// col
$pipeline = $this->getCollection() -> createPipeline();
$pMatchArr = array(
'ij_statistics_time' => array(
'$gte' => $_intStartTime,
'$lte' => $_intEndTime
)
);
if(!empty($_intUid)){
$pMatchArr['ij_uid'] = $_intUid;
}
$pipeline->match($pMatchArr);
// get date
$pipeline = $pipeline->group(
array(
'_id'=>'$ij_uid',
'level_one_count'=>array( '$sum'=>'$ij_level_one_new_num' ),
'level_two_count'=>array( '$sum'=>'$ij_level_two_new_num' ),
'level_three_count'=>array( '$sum'=>'$ij_level_three_new_num' ),
'level_four_count'=>array( '$sum'=>'$ij_level_four_new_num' ),
'level_five_count'=>array( '$sum'=>'$ij_level_five_new_num' ),
'level_six_count'=>array( '$sum'=>'$ij_level_six_new_num' ),
'level_seven_count'=>array( '$sum'=>'$ij_level_seven_new_num' ),
'level_eight_count'=>array( '$sum'=>'$ij_level_eight_new_num' ),
'level_nine_count'=>array( '$sum'=>'$ij_level_nine_new_num' ),
'level_ten_count'=>array( '$sum'=>'$ij_level_ten_new_num' ),
)
);
$pipeArr = $pipeline->aggregate();
return $pipeArr;
看起来它不起作用
$pipeArr = $cursor->groupBy( 'ij_uid' )
->aggregate( 'sum' , $fileds)
->get( $fileds );
return $pipeArr;
我使用流明5.2和jenssegers / laravel-mongodb
答案 0 :(得分:1)
我使用以下方法来解决问题,可以在orm本机语句中使用
$pipeArr = self::raw( function($collection) use ($match ) {
return $collection->aggregate([
['$match' => $match],
[
'$group' => [
'_id' => '$ij_uid',
'level_one_count' => [ '$sum' => '$ij_level_one_new_num'],
'level_two_count' => [ '$sum' => '$ij_level_two_new_num'],
'level_three_count' => [ '$sum' => '$ij_level_three_new_num'],
'level_four_count' => [ '$sum' => '$ij_level_four_new_num' ],
'level_five_count' => [ '$sum' => '$ij_level_five_new_num' ],
'level_six_count' => [ '$sum' => '$ij_level_six_new_num' ],
'level_seven_count' => [ '$sum' => '$ij_level_seven_new_num' ],
'level_eight_count' => [ '$sum' => '$ij_level_eight_new_num' ],
'level_nine_count' => [ '$sum' => '$ij_level_nine_new_num' ],
'level_ten_count' => [ '$sum' => '$ij_level_ten_new_num' ]
]
],
['$sort' => ['ij_uid' => 1]],
]);
});
return $pipeArr;