php f3框架使用聚合框架管道与mongo mapper

时间:2017-05-16 05:36:30

标签: php fat-free-framework

我想在事务表中使用mongo mapper起诉聚合框架管道,结果应该是这样的: 假设我在status 1,2,3,4 (1=pending, 2=confirmed, 3=decline, 4=claimed)

的集合中有7000个事务

数据表示:

Pending Count | Confirmed Count | Decline Count | Claimed Count
 4000                 2000            1000          0

1。 问题是我不确定哪个字段应该用于组中的密钥。

  1. 我的代码将返回符合条件的所有记录。
  2. 请看一下我的代码:

    $filters = array('dateCreated'=>array(
                                '$gte'=>'2015-01-01', 
                                '$lte'=>'2017-05-15'
                        ));
    
     $group = [
                  'keys' =>['userId'=>1],
                  'initial' => ['pendingCount'=>0, 'confirmedCount'=>0, 'declineCount'=>0, 'claimedCount'=>0],
                  'reduce'=>'
                          function(obj, result){
    
                            switch(result.status){
                                case 1:
                                    result.pendingCount++;
                                    break;
    
                                case 2:
                                    result.confirmedCount++;
                                    break;                          
    
                                case 3:
                                    result.declineCount++;
                                    break;
                                case 4:
                                    result.claimedCount++;
                                    break;
    
                            }
                          }
                            ',
                  'finalize'=>'function(result){}'
            ];
    
     $data =  $this->mapper->find($filter, ['group'=>$group]);
     echo "data<pre>";
     print_r($data);
    

    输出:

    在这里,您看到它返回了多个文档,但我只想要一条记录用于所有事务,可能是键userId应该替换为“null”

      

    阵列(       [0] =&gt;排列           (               [_id] =&gt; MongoId对象                   (                       [$ id] =&gt; 591a8f5bb06b243d0262d0a1                   )

            [userId] => 
            [pendingCount] => 0
            [confirmedCount] => 0
            [declineCount] => 0
            [claimedCount] =>0
        )
    
    [1] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 591a8f5bb06b243d0262d0a2
                )
    
            [userId] => 
            [pendingCount] => 0
            [confirmedCount] => 0
            [declineCount] => 0
            [claimedCount] =>0
        )
    
    [2] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 591a8f5bb06b243d0262d0a3
                )
    
            [userId] => 38730976
            [pendingCount] => 0
            [confirmedCount] => 0
            [declineCount] => 0
            [claimedCount] =>0
        )
    
    [3] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 591a8f5bb06b243d0262d0a4
                )
    
            [userId] => 64920
            [pendingCount] => 0
            [confirmedCount] => 0
            [declineCount] => 0
            [claimedCount] =>0
        )
    
         

    ...

1 个答案:

答案 0 :(得分:1)

您按//*[starts-with(name(), 'h')] 进行分组,因此每userId行可获得一行。只需传递一个空的userId数组来聚合所有记录的数据。你的缩减函数也有一个拼写错误:它应该是keys

obj.status