Mongo聚合多个匹配/组

时间:2017-08-14 08:55:59

标签: php mongodb match aggregation-framework aggregate

我有商店数据,report_type是每种类型的数据
  - 类型3存储客户购买多少钱和多少交易   - 类型5存储了多少人和他们的平均年龄 我需要一个像这样的数组:

"report_type3"       
    (
        [_id] => 2441
        [amountclient] => 3749078452,
        [trans] => 1000
    ), 
    (
        [_id] => 2442
        [amountclient] => 15533754,
        [trans] => 900
    )            
,
"report_type5"
        (
            [_id] => 2441
            [people] => 120,
            [avgage] => 23,
        ),
       (
            [_id] => 2441
            [people] => 120,
            [avgage] => 23,
        )

我的查询看起来像这样,但它不起作用

$cursor = $collection->aggregate([
            ['$match' => [ 'id_station' => ['$in' => $store ], 'report_type' => 3,
                            'date' => ['$gte'=> new MongoDB\BSON\UTCDateTime(strtotime("2017-07-01")*1000), '$lte'=> new MongoDB\BSON\UTCDateTime(strtotime("2017-08-05")*1000)]  
                         ] 
            ],            
            ['$group' => [
                '_id' => [ 'id_station' => '$id_station', 'receiptid' => '$receiptid' ],
                'amount' => [ '$sum' => '$amount' ]                
            ]], .... *I group by receiptid because there are many items in one clients.*
            [ '$group' => [
                '_id' => '$_id.id_station',
                'amount' => [ '$sum' => '$amount' ],
        'trans' => [ '$sum' => '$trans' ]                
            ]],
        [
                '$project' => [                    
                    'report_type3' => ['_id','$amountclient','$trans']
                ]
            ], 

            ['$match' => [ 'id_station' => ['$in' => $store ], 'report_type' => 5,
                            'date' => ['$gte'=> new MongoDB\BSON\UTCDateTime(strtotime("2017-07-01")*1000), '$lte'=> new MongoDB\BSON\UTCDateTime(strtotime("2017-08-05")*1000)]  
                         ] 
            ],
        [ '$group' => [
                '_id' => ['$_id.id_station', 'area_type' => '$area_type'],
                'people' => [ '$sum' => '$people' ],
        'avgage' => [ '$avg' => '$age' ]                
            ]],
            [
                '$project' => [                    
                    'report_type5' => [ '_id', 'people', 'avgage' ]
                ]
            ]
        ]);

你能帮帮我吗?

0 个答案:

没有答案