yii\mongodb\QueryBuilder::mapReduce PHP Method

mapReduce() public method

Generates 'map-reduce' command.
See also: https://docs.mongodb.com/manual/core/map-reduce/
public mapReduce ( string $collectionName, MongoDB\BSON\Javascript | string $map, MongoDB\BSON\Javascript | string $reduce, string | array $out, array $condition = [], array $options = [] ) : array
$collectionName string collection name.
$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 filter condition for including a document in the aggregation.
$options array 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: int, the maximum number of documents to return in the collection. - finalize: \MongoDB\BSON\Javascript|string, 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: bool, specifies whether to convert intermediate data into BSON format between the execution of the map and reduce functions. - verbose: bool, specifies whether to include the timing information in the result information.
return array command document.
    public function mapReduce($collectionName, $map, $reduce, $out, $condition = [], $options = [])
    {
        if (!$map instanceof Javascript) {
            $map = new Javascript((string) $map);
        }
        if (!$reduce instanceof Javascript) {
            $reduce = new Javascript((string) $reduce);
        }
        $document = ['mapReduce' => $collectionName, 'map' => $map, 'reduce' => $reduce, 'out' => $out];
        if (!empty($condition)) {
            $document['query'] = $this->buildCondition($condition);
        }
        if (!empty($options)) {
            $document = array_merge($document, $options);
        }
        return $document;
    }