public function aggregate(array $pipeline, array $options = [])
{
$hasOutStage = Functions::isLastPipelineOperatorOut($pipeline);
/* A "majority" read concern is not compatible with the $out stage, so
* avoid providing the Collection's read concern if it would conflict.
*/
if (!isset($options['readConcern']) && !($hasOutStage && $this->readConcern->getLevel() === ReadConcern::MAJORITY)) {
$options['readConcern'] = $this->readConcern;
}
if (!isset($options['readPreference'])) {
$options['readPreference'] = $this->readPreference;
}
if ($hasOutStage) {
$options['readPreference'] = new ReadPreference(ReadPreference::RP_PRIMARY);
}
if (!isset($options['typeMap']) && (!isset($options['useCursor']) || $options['useCursor'])) {
$options['typeMap'] = $this->typeMap;
}
$operation = new Aggregate($this->databaseName, $this->collectionName, $pipeline, $options);
$server = $this->manager->selectServer($options['readPreference']);
return $operation->execute($server);
}