From 49a70dc31173fea57d7b181bd7e4bac05fbad695 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Sun, 1 Dec 2013 21:20:34 +0200 Subject: [PATCH] Mongo aggregation functions added as draft. --- extensions/mongo/Collection.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/extensions/mongo/Collection.php b/extensions/mongo/Collection.php index 7700233..7d3e2a5 100644 --- a/extensions/mongo/Collection.php +++ b/extensions/mongo/Collection.php @@ -191,6 +191,46 @@ class Collection extends Object } /** + * Returns a list of distinct values for the given column across a collection. + * @param string $column column to use. + * @param array $condition query parameters. + * @return array|boolean array of distinct values, or "false" on failure. + */ + public function distinct($column, $condition = []) + { + return $this->mongoCollection->distinct($column, $this->buildCondition($condition)); + } + + /** + * @param $pipeline + * @param array $pipelineOperator + * @return array + */ + public function aggregate($pipeline, $pipelineOperator = []) + { + $args = func_get_args(); + return call_user_func_array([$this->mongoCollection, 'aggregate'], $args); + } + + /** + * @param mixed $keys + * @param array $initial + * @param \MongoCode|string $reduce + * @param array $options + * @return array + */ + public function mapReduce($keys, $initial, $reduce, $options = []) + { + if (!($reduce instanceof \MongoCode)) { + $reduce = new \MongoCode($reduce); + } + if (array_key_exists('condition', $options)) { + $options['condition'] = $this->buildCondition($options['condition']); + } + return $this->mongoCollection->group($keys, $initial, $reduce, $options); + } + + /** * Checks if command execution result ended with an error. * @param mixed $result raw command execution result. * @throws Exception if an error occurred.