环境
mongos 3.0.14
使用 aggregate 能够实现较为复杂的数据聚合操做,例如 汇总(count)、去重汇总(distinct count)、分组统计(group having)等。mongodb
aggregate 返回结果为数组,须要注意数据大小不能超过16M。数组
例如:code
$pipeline = [ ['$match' => $tmpCondition], ['$group' => [ '_id' => ['user_id'=>'$user_id'] ]], ['$group' => [ '_id' => '_id.user_id', 'number' => ['$sum'=>1] ]] ]; $options = [ 'allowDiskUse'=>true, 'cursor'=>['batchSize'=>1] ]; $data = MongoSvc::get('user')->user_info->aggregate($pipeline,$options);
对于大量返回结果的聚合,可使用 aggregateCursor 返回游标,能够避免数据大小超限。排序
aggregateCursor 的返回结果为游标,可循环取数。索引
例如:ip
$pipeline = [ ['$match' => $matchArr], ['$project' => ['id'=>1,'_id'=>0]], ['$group' => [ '_id' => '$id', 'count' => ['$sum' => 1] ]], ['$match' => [ 'count' => ['$gt' => 1] ]] ]; //这里改成aggregateCursor用游标循环获取 $data = MongoSvc::get('user')->user_info->aggregateCursor($pipeline);
https://docs.mongodb.com/manu...文档
https://docs.mongodb.com/manu...get
汇总统计文档中某个字段(如'sum')的count值:string
$pipeline = [ ['$match' => $tmpCondition], ['$group' => [ '_id' => ['sum'], 'sum_value' => ['$sum' => '$money'] ]] ];
某列的去重后的数据:it
$pipeline = [ ['$match' => $tmpCondition], ['$group' => [ '_id' => ['user_id' => '$user_id'] ]] ];
统计某列(如'user_id')去重后的count值:
$pipeline = [ ['$match' => $tmpCondition], ['$group' => [ '_id' => ['user_id'=>'$user_id'] ]], ['$group' => [ '_id' => '_id.user_id', 'number' => ['$sum'=>1] ]] ]; $pipeline = [ ['$match' => $tmpCondition], ['$group' => [ '_id' => ['qid' => '$qid'], 'max_number' => ['$max' => '$days'] ]], ['$group' => [ '_id' => ['number' => '$max_number'], 'total' => ['$sum' => 1] ]] ];
统计分组后,各组内的某列汇总值:
$pipeline = [ ['$match' => $tmpCondition], ['$group' => [ '_id' => ['type' => '$type'], 'sum_value' => ['$sum' => '$number'] ]] ];