mongodb 分類聚合 (配合YII2框架)

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

]

]

]);

有三年沒寫博客了

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章