php mongoDB aggregate() возвращает объект MongoDB\Driver\Cursor вместо результата
Я работаю над переходом на новый драйвер php с mongodb, в данный момент я понятия не имею, в чем именно проблема, запрос работает со старым драйвером, на самом деле я не получаю ни запроса, ни ошибки php. Print_r возвращает serult агрегата 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 ответ
Решение
aggregate
метод возвращает Cursor
см. документацию для получения дополнительной информации.
Если вам нужно использовать результат как array
ты можешь использовать iterator_to_array
, как это:
$resultAsArray = iterator_to_array( $h->aggregate($aggregateArr), false );
Сейчас, $resultAsArray
является array
,