From 7314b5fe7e7cea78f5919a5030c8e661b588f3c6 Mon Sep 17 00:00:00 2001 From: Paul Klimov Date: Fri, 6 Dec 2013 11:24:44 +0200 Subject: [PATCH] Options parameter added to "\yii\mogo\Collection::mapReduce()" --- extensions/mongo/Collection.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/extensions/mongo/Collection.php b/extensions/mongo/Collection.php index e9c37b8..19c84bc 100644 --- a/extensions/mongo/Collection.php +++ b/extensions/mongo/Collection.php @@ -522,10 +522,17 @@ class Collection extends Object * @param string|array $out output collection name. It could be a string for simple output * ('outputCollection'), or an array for parametrized output (['merge' => 'outputCollection']) * @param array $condition criteria for including a document in the aggregation. + * @param array $options additional optional parameters to the mapReduce command. Valid options include: + * - sort - array - key to sort the input documents. The sort key must be in an existing index for this collection. + * - limit - the maximum number of documents to return in the collection. + * - finalize - function, which follows the reduce method and modifies the output. + * - scope - array - specifies global variables that are accessible in the map, reduce and finalize functions. + * - jsMode - boolean -Specifies whether to convert intermediate data into BSON format between the execution of the map and reduce functions. + * - verbose - boolean - specifies whether to include the timing information in the result information. * @return string the map reduce output collection name. * @throws Exception on failure. */ - public function mapReduce($map, $reduce, $out, $condition = []) + public function mapReduce($map, $reduce, $out, $condition = [], $options = []) { if (!($map instanceof \MongoCode)) { $map = new \MongoCode((string)$map); @@ -542,6 +549,14 @@ class Collection extends Object if (!empty($condition)) { $command['query'] = $this->buildCondition($condition); } + if (array_key_exists('finalize', $options)) { + if (!($options['finalize'] instanceof \MongoCode)) { + $options['finalize'] = new \MongoCode((string)$options['finalize']); + } + } + if (!empty($options)) { + $command = array_merge($command, $options); + } $token = $this->composeLogToken('mapReduce', [$map, $reduce, $out]); Yii::info($token, __METHOD__); try {