MongoDB 原始語句
db.issues.aggregate([{$group : {_id : {"raise_org":'$raise_org'}, 'product' : {$push : '$product'},'qtype' : {$push : '$qtype'}}}])
利用YII2構造器
$match = [
'$match' => [
'created_at'=>[
'$gte'=>$strstarttime,
'$lt'=>$strendtime
],
]
];
$issuesrescount = Issue::getCollection()->aggregate([
$match,
[
'$group'=>[
'_id'=>['raise_org'=>'$raise_org'],
'count'=>[
'$sum'=>1
]
],
]
]);
$itemcount = $issuesrescount[0]['count'];
$issuesres = Issue::getCollection()->aggregate([
$match,
[
'$group'=>[
'_id'=>['raise_org'=>'$raise_org'], //通過user_id分組去重
'product'=>[
'$push'=>'$product'
],
'qtype'=>[
'$push'=>'$qtype'
],
'count'=>[
'$sum'=>1
],
],
],
['$skip'=>(int)($page-1)*$pagesize],
['$limit'=>(int)$pagesize],
[
'$sort'=>[
'count'=>-1
]
]
]);
有三年沒寫博客了