2

我正在从 mongodb 迁移到新的 php 驱动程序,此时我不知道到底是什么问题,查询在旧驱动程序上运行良好,实际上我没有得到任何查询或 php 错误。print_r 返回聚合 fn 的 serult 如下 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;
4

1 回答 1

6

aggregate方法返回 a Cursor,有关详细信息,请参阅文档

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

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

现在,$resultAsArray是一个array.

于 2018-07-20T08:20:31.560 回答