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

execute() public method

Execute the operation.
See also: Executable::execute()
public execute ( MongoDB\Driver\Server $server ) : mixed[]
$server MongoDB\Driver\Server
return mixed[]
    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());
        if (!isset($result->values) || !is_array($result->values)) {
            throw new UnexpectedValueException('distinct command did not return a "values" array');
        }
        return $result->values;
    }

Usage Example

Beispiel #1
0
 /**
  * Finds the distinct values for a specified field across the collection.
  *
  * @see Distinct::__construct() for supported options
  *
  * @param string       $fieldName Field for which to return distinct values
  * @param array|object $filter Query by which to filter documents
  * @param array        $options Command options
  *
  * @return mixed[]
  */
 public function distinct($fieldName, $filter = [], array $options = [])
 {
     if (!isset($options['readConcern'])) {
         $options['readConcern'] = $this->readConcern;
     }
     if (!isset($options['readPreference'])) {
         $options['readPreference'] = $this->readPreference;
     }
     $operation = new Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options);
     $server = $this->manager->selectServer($options['readPreference']);
     return $operation->execute($server);
 }