Phalcon\Db\Adapter\MongoDB\Operation\Count::execute PHP Method

execute() public method

Execute the operation.
See also: Executable::execute()
public execute ( MongoDB\Driver\Server $server ) : integer
$server MongoDB\Driver\Server
return integer
    public function execute(Server $server)
    {
        $readPreference = isset($this->options['readPreference']) ? $this->options['readPreference'] : null;
        $cursor = $server->executeCommand($this->databaseName, $this->createCommand($server), $readPreference);
        $result = current($cursor->toArray());
        // Older server versions may return a float
        if (!isset($result->n) || !(is_integer($result->n) || is_float($result->n))) {
            throw new UnexpectedValueException('count command did not return a numeric "n" value');
        }
        return (int) $result->n;
    }

Usage Example

Beispiel #1
0
 /**
  * Gets the number of documents matching the filter.
  *
  * @see Count::__construct() for supported options
  *
  * @param array|object $filter Query by which to filter documents
  * @param array        $options Command options
  *
  * @return integer
  */
 public function count($filter = [], array $options = [])
 {
     if (!isset($options['readConcern'])) {
         $options['readConcern'] = $this->readConcern;
     }
     if (!isset($options['readPreference'])) {
         $options['readPreference'] = $this->readPreference;
     }
     $operation = new Count($this->databaseName, $this->collectionName, $filter, $options);
     $server = $this->manager->selectServer($options['readPreference']);
     return $operation->execute($server);
 }