yii\mongodb\Query::aggregate PHP Method

aggregate() protected method

Performs the aggregation for the given column.
protected aggregate ( string $column, string $operator, Connection $db ) : integer
$column string column name.
$operator string aggregation operator.
$db Connection the database connection used to execute the query.
return integer aggregation result.
    protected function aggregate($column, $operator, $db)
    {
        $collection = $this->getCollection($db);
        $pipelines = [];
        if ($this->where !== null) {
            $pipelines[] = ['$match' => $this->where];
        }
        $pipelines[] = ['$group' => ['_id' => '1', 'total' => ['$' . $operator => '$' . $column]]];
        $result = $collection->aggregate($pipelines);
        if (array_key_exists(0, $result)) {
            return $result[0]['total'];
        } else {
            return 0;
        }
    }