php mongoDB aggregate() 返回 MongoDB\Driver\Cursor 对象而不是结果

php mongoDB aggregate() returning MongoDB\Driver\Cursor Object instead of result

我正在从 mongodb 迁移到新的 php 驱动程序,此时我不知道问题到底是什么,查询在旧驱动程序上运行良好,实际上我没有收到任何查询或 php 错误。 print_r returns聚合fn的结果如下 php 版本:7.1.18, Zend 引擎: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;

aggregate方法returns一个Cursor,参见documentation获取更多信息。

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

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

现在,$resultAsArrayarray