mapReduce() public method
Performs aggregation using MongoDB "map-reduce" mechanism.
Note: this function will not return the aggregation result, instead it will write it inside the another Mongo collection specified by "out" parameter. For example:
$customerCollection = Yii::$app->mongo->getCollection('customer');
$resultCollectionName = $customerCollection->mapReduce(
'function () {emit(this.status, this.amount)}',
'function (key, values) {return Array.sum(values)}',
'mapReduceOut',
['status' => 3]
);
$query = new Query();
$results = $query->from($resultCollectionName)->all();
| public string|array mapReduce ( $map, $reduce, $out, $condition = [], $options = [] ) | ||
|---|---|---|
| $map | \MongoDB\BSON\Javascript|string |
Function, which emits map data from collection. Argument will be automatically cast to \MongoDB\BSON\Javascript. |
| $reduce | \MongoDB\BSON\Javascript|string |
Function that takes two arguments (the map key and the map values) and does the aggregation. Argument will be automatically cast to \MongoDB\BSON\Javascript. |
| $out | string|array |
Output collection name. It could be a string for simple output ('outputCollection'), or an array for parametrized output (['merge' => 'outputCollection']). You can pass ['inline' => true] to fetch the result at once without temporary collection usage. |
| $condition | array |
Criteria for including a document in the aggregation. |
| $options | array |
Additional optional parameters to the mapReduce command. Valid options include:
|
| return | string|array |
The map reduce output collection name or output results. |
| throws | yii\mongodb\Exception |
on failure. |
Please login to continue.