PHP MongoDB Aggregate()返回MongoDB \ Driver \ Cursor对象而不是结果

时间:2018-07-20 04:27:59

标签: php mongodb php-7.2 php-mongodb

我正在从mongodb迁移到新的php驱动程序,这时我不知道到底是什么问题,查询与旧驱动程序很好地工作,实际上我没有任何查询或php错误。 print_r返回汇总fn的序列,如下所示 php版本:7.1.18, Zend Engine:v3.1.0, mongo:v3.4.15

> MongoDB\Driver\Cursor Object (
>     [database] => demoDb
>     [collection] => 
>     [query] => 
>     [command] => MongoDB\Driver\Command Object
>         (
>             [command] => stdClass Object
>                 (
>                     [aggregate] => app_button
>                     [pipeline] => Array
>                         (
>                             [0] => stdClass Object
>                                 (
>                                     [$match] => stdClass Object
>                                         (
>                                             [location] => stdClass Object
>                                                 (
>                                                     [$in] => Array
>                                                         (
>                                                             [0] => 2978
>                                                             [1] => 2979
>                                                             [2] => 2980
>                                                             [3] => 2981
>                                                             [4] => 3060
>                                                             [5] => 3061
>                                                             [6] => 3062
>                                                             [7] => 3063
>                                                             [8] => 3064
>                                                             [9] => 3065
>                                                             [10] => 3066
>                                                         
>                                                         )
> 
>                                                 )
> 
>                                             [language] => stdClass Object
>                                                 (
>                                                     [$in] => Array
>                                                         (
>                                                             [0] => multiselect-all
>                                                             [1] => en
>                                                             [2] => gr
>                                                             [3] => zh
>                                                             [4] => vi
>                                                         )
> 
>                                                 )
> 
>                                             [$or] => Array
>                                                 (
>                                                     [0] => stdClass Object
>                                                         (
>                                                             [hittime] => stdClass Object
>                                                                 (
>                                                                     [$regex] => 2018-07-
>                                                                 )
> 
>                                                         )
> 
>                                                 )
> 
>                                         )
> 
>                                 )
> 
>                             [1] => stdClass Object
>                                 (
>                                     [$group] => stdClass Object
>                                         (
>                                             [_id] => $action
>                                             [count] => stdClass Object
>                                                 (
>                                                     [$sum] => 1
>                                                 )
> 
>                                             [totalvalue] => stdClass Object
>                                                 (
>                                                     [$sum] => $total_value
>                                                 )
> 
>                                             [optionvalue] => stdClass Object
>                                                 (
>                                                     [$sum] => $option_value
>                                                 )
> 
>                                             [recvalue] => stdClass Object
>                                                 (
>                                                     [$sum] => $rec_value
>                                                 )
> 
>                                             [data_used] => stdClass Object
>                                                 (
>                                                     [$sum] => $data_used
>                                                 )
> 
>                                         )
> 
>                                 )
> 
>                             [2] => stdClass Object
>                                 (
>                                     [$sort] => stdClass Object
>                                         (
>                                             [_id] => 1
>                                         )
> 
>                                 )
> 
>                         )
> 
>                     [allowDiskUse] => 
>                     [cursor] => stdClass Object
>                         (
>                         )
> 
>                 )
> 
>         )
> 
>     [readPreference] => MongoDB\Driver\ReadPreference Object
>         (
>             [mode] => primary
>         )
> 
>     [session] => 
>     [isDead] => 
>     [currentIndex] => 0
>     [currentDocument] => 
>     [server] => MongoDB\Driver\Server Object
>         (
>             [host] => 127.0.0.1
>             [port] => 27017
>             [type] => 1
>             [is_primary] => 
>             [is_secondary] => 
>             [is_arbiter] => 
>             [is_hidden] => 
>             [is_passive] => 
>             [last_is_master] => Array
>                 (
>                     [ismaster] => 1
>                     [maxBsonObjectSize] => 16777216
>                     [maxMessageSizeBytes] => 48000000
>                     [maxWriteBatchSize] => 1000
>                     [localTime] => MongoDB\BSON\UTCDateTime Object
>                         (
>                             [milliseconds] => 1532060099520
>                         )
> 
>                     [maxWireVersion] => 5
>                     [minWireVersion] => 0
>                     [readOnly] => 
>                     [ok] => 1
>                 )
> 
>             [round_trip_time] => 0
>         )
> 
> )

代码

$collection = array();
        $client = new MongoDB\Client;
        $h = $client->selectDatabase('demoDb')->selectCollection($params['tablename']);
 $aggregateArr = array(
                array(
                    '$match' => array(
                        "location" => array('$in' => $params['locations'],
                        ),
                        "language" => array('$in' => $params['languages'],
                        ),
                        '$or' => $orarray,
                    ),
                ),
                array(
                    '$group' => array(
                        '_id' => $params['groupid'],
                        'count' => array('$sum' => 1),
                        'totalvalue' => array('$sum' => '$total_value'),
                        'optionvalue' => array('$sum' => '$option_value'),
                        'recvalue' => array('$sum' => '$rec_value'),
                        'data_used' => array('$sum' => '$data_used'),
                    ),
                ));
$collection = $h->aggregate($aggregateArr);
            print_r($collection);
            exit;

1 个答案:

答案 0 :(得分:1)

aggregate方法返回一个Cursor,有关更多信息,请参见documentation

如果您需要将结果用作array,则可以使用iterator_to_array,如下所示:

$resultAsArray = iterator_to_array( $h->aggregate($aggregateArr), false );

现在,$resultAsArrayarray