Commit e3c281f3 by Paul Klimov

Mongo Collection "aggregate" method interface refactored.

parent a3f5236e
......@@ -349,19 +349,27 @@ class Collection extends Object
/**
* Performs aggregation using Mongo Aggregation Framework.
* @param array $pipeline list of pipeline operators, or just the first operator
* @param array $pipelineOperator Additional pipeline operators
* @param array $pipelineOperator additional pipeline operator. You can specify additional
* pipelines via third argument, fourth argument etc.
* @return array the result of the aggregation.
* @throws Exception on failure.
* @see http://docs.mongodb.org/manual/applications/aggregation/
*/
public function aggregate($pipeline, $pipelineOperator = [])
{
$token = 'Aggregating from ' . $this->getFullName();
Yii::info($token, __METHOD__);
Yii::beginProfile($token, __METHOD__);
$args = func_get_args();
$result = call_user_func_array([$this->mongoCollection, 'aggregate'], $args);
Yii::endProfile($token, __METHOD__);
return $result;
try {
Yii::beginProfile($token, __METHOD__);
$args = func_get_args();
$result = call_user_func_array([$this->mongoCollection, 'aggregate'], $args);
$this->tryResultError($result);
Yii::endProfile($token, __METHOD__);
return $result['result'];
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
......@@ -377,8 +385,8 @@ class Collection extends Object
* - condition - criteria for including a document in the aggregation.
* - finalize - function called once per unique key that takes the final output of the reduce function.
* @return array the result of the aggregation.
* @see http://docs.mongodb.org/manual/reference/command/group/
* @throws Exception on failure.
* @see http://docs.mongodb.org/manual/reference/command/group/
*/
public function group($keys, $initial, $reduce, $options = [])
{
......
......@@ -244,8 +244,8 @@ class Query extends Component implements QueryInterface
]
];
$result = $collection->aggregate($pipelines);
if (!empty($result['ok'])) {
return $result['result'][0]['total'];
if (array_key_exists(0, $result)) {
return $result[0]['total'];
} else {
return 0;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment