Sokil\Mongo\Collection::aggregate PHP Method

aggregate() public method

Aggregate using pipeline
public aggregate ( callable | array | Pipeline $pipeline, array $options = [], boolean $asCursor = false ) : array
$pipeline callable | array | Pipeline list of pipeline stages
$options array
$asCursor boolean return result as cursor
return array result of aggregation
    public function aggregate($pipeline, array $options = array(), $asCursor = false)
    {
        // configure through callable
        if (is_callable($pipeline)) {
            $pipelineConfiguratorCallable = $pipeline;
            $pipeline = $this->createAggregator();
            call_user_func($pipelineConfiguratorCallable, $pipeline);
        }
        // get aggregation array
        if ($pipeline instanceof Pipeline) {
            if ($options && is_array($options)) {
                $options = array_merge($pipeline->getOptions(), $options);
            } else {
                $options = $pipeline->getOptions();
            }
            $pipeline = $pipeline->toArray();
        } else {
            if (!is_array($pipeline)) {
                throw new Exception('Wrong pipeline specified');
            }
        }
        // log
        $client = $this->database->getClient();
        if ($client->isDebugEnabled()) {
            // record pipeline
            if ($client->hasLogger()) {
                $client->getLogger()->debug(get_called_class() . ':<br><b>Pipeline</b>:<br>' . json_encode($pipeline));
            }
            // Check options only in debug mode. In production common exception will raised
            if ($options) {
                // get db version
                $dbVersion = $client->getDbVersion();
                // check options for db < 2.6
                if (version_compare($dbVersion, '2.6.0', '<')) {
                    if (!empty($options['explain'])) {
                        throw new FeatureNotSupportedException('Explain of aggregation implemented only from 2.6.0');
                    }
                    if (!empty($options['allowDiskUse'])) {
                        throw new FeatureNotSupportedException('Option allowDiskUse of aggregation implemented only from 2.6.0');
                    }
                    if (!empty($options['cursor'])) {
                        throw new FeatureNotSupportedException('Option cursor of aggregation implemented only from 2.6.0');
                    }
                }
                // check options for db < 3.2
                if (version_compare($dbVersion, '3.2.0', '<')) {
                    if (!empty($options['bypassDocumentValidation'])) {
                        throw new FeatureNotSupportedException('Option bypassDocumentValidation of aggregation implemented only from 3.2.0');
                    }
                    if (!empty($options['readConcern'])) {
                        throw new FeatureNotSupportedException('Option readConcern of aggregation implemented only from 3.2.0');
                    }
                }
            }
        }
        // return result as cursor
        if ($asCursor) {
            if (version_compare(\MongoClient::VERSION, '1.5.0', '<')) {
                throw new FeatureNotSupportedException('Aggregate cursor supported from driver version 1.5');
            }
            $cursor = $this->getMongoCollection()->aggregateCursor($pipeline, $options);
            return $cursor;
        }
        // prepare command
        $command = array('aggregate' => $this->getName(), 'pipeline' => $pipeline);
        // add options
        if ($options) {
            $command += $options;
        }
        // aggregate
        $status = $this->database->executeCommand($command);
        if ($status['ok'] != 1) {
            throw new Exception('Aggregate error: ' . $status['errmsg']);
        }
        // explain response
        if (!empty($command['explain'])) {
            return $status['stages'];
        }
        // result response
        return $status['result'];
    }

Usage Example

Example #1
0
 public function testAggregate_ResultAsCursor()
 {
     $this->collection->createDocument(array('param' => 1))->save();
     $this->collection->createDocument(array('param' => 2))->save();
     $this->collection->createDocument(array('param' => 3))->save();
     $this->collection->createDocument(array('param' => 4))->save();
     $pipeline = $this->collection->createAggregator()->match(array('param' => array('$gte' => 2)))->group(array('_id' => 0, 'sum' => array('$sum' => '$param')));
     $result = $this->collection->aggregate($pipeline, array(), true);
     $this->assertInstanceOf('\\MongoCommandCursor', $result);
 }
All Usage Examples Of Sokil\Mongo\Collection::aggregate