lithium\data\source\MongoDb::calculation()

public method

Executes calculation-related queries, such as those required for count.

Parameters

  • string $type

    Only accepts count.

  • mixed $query

    The query to be executed.

  • array $options

    Optional arguments for the read() query that will be executed to obtain the calculation result.

Returns

integer

Result of the calculation.

Source

	public function calculation($type, $query, array $options = []) {
		$query->calculate($type);

		switch ($type) {
			case 'count':
				$args = $query->export($this);

				$pipeline = [];
				if ($args['conditions']) {
					$pipeline[] = ['$match' => $args['conditions']];
				}
				$pipeline[] = ['$group' => ['_id' => null, 'count' => ['$sum' => 1]]];

				$result = $this->manager->executeCommand($this->_config['database'], new Command([
					'aggregate' => $args['source'],
					'pipeline' => $pipeline,
					'cursor' => new stdClass,
				]))->toArray();

				return (!empty($result)) ? current($result)->count : 0;
		}
	}