serverSupportsFeature() public static method

Return whether the server supports a particular feature.
public static serverSupportsFeature ( MongoDB\Driver\Server $server, integer $feature ) : boolean
$server MongoDB\Driver\Server Server to check
$feature integer Feature constant (i.e. wire protocol version)
return boolean
Example #1
0
 /**
  * Execute the operation.
  *
  * For servers < 2.6, this will actually perform an insert operation on the
  * database's "system.indexes" collection.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return string[] The names of the created indexes
  */
 public function execute(Server $server)
 {
     if (Functions::serverSupportsFeature($server, self::$wireVersionForCommand)) {
         $this->executeCommand($server);
     } else {
         $this->executeLegacy($server);
     }
     return array_map(function (IndexInput $index) {
         return (string) $index;
     }, $this->indexes);
 }
Example #2
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return InsertOneResult
  */
 public function execute(Server $server)
 {
     $options = [];
     if (isset($this->options['bypassDocumentValidation']) && Functions::serverSupportsFeature($server, self::$wireVersionForDocumentLevelValidation)) {
         $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
     }
     $bulk = new Bulk($options);
     $insertedId = $bulk->insert($this->document);
     if ($insertedId === null) {
         $insertedId = Functions::extractIdFromInsertedDocument($this->document);
     }
     $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
     $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
     return new InsertOneResult($writeResult, $insertedId);
 }
Example #3
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return CollectionInfoIterator
  */
 public function execute(Server $server)
 {
     return Functions::serverSupportsFeature($server, self::$wireVersionForCommand) ? $this->executeCommand($server) : $this->executeLegacy($server);
 }
Example #4
0
 /**
  * Finds a single document and updates it, returning either the original or
  * the updated document.
  *
  * The document to return may be null if no document matched the filter. By
  * default, the original document is returned. Specify
  * FindOneAndUpdate::RETURN_DOCUMENT_AFTER for the "returnDocument" option
  * to return the updated document.
  *
  * Note: BSON deserialization of the returned document does not yet support
  * a custom type map (depends on: https://jira.mongodb.org/browse/PHPC-314).
  *
  * @see FindOneAndReplace::__construct() for supported options
  * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
  *
  * @param array|object $filter Query by which to filter documents
  * @param array|object $update Update to apply to the matched document
  * @param array        $options Command options
  *
  * @return object|null
  */
 public function findOneAndUpdate($filter, $update, array $options = [])
 {
     $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
     if (!isset($options['writeConcern']) && Functions::serverSupportsFeature($server, self::$wireVersionForFindAndModifyWriteConcern)) {
         $options['writeConcern'] = $this->writeConcern;
     }
     $operation = new FindOneAndUpdate($this->databaseName, $this->collectionName, $filter, $update, $options);
     return $operation->execute($server);
 }
Example #5
0
 /**
  * Create the distinct command.
  *
  * @param Server $server
  *
  * @return Command
  */
 private function createCommand(Server $server)
 {
     $cmd = ['distinct' => $this->collectionName, 'key' => $this->fieldName];
     if (!empty($this->filter)) {
         $cmd['query'] = (object) $this->filter;
     }
     if (isset($this->options['maxTimeMS'])) {
         $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
     }
     if (isset($this->options['readConcern']) && Functions::serverSupportsFeature($server, self::$wireVersionForReadConcern)) {
         $cmd['readConcern'] = Functions::readConcernAsDocument($this->options['readConcern']);
     }
     return new Command($cmd);
 }
Example #6
0
 /**
  * Create the count command.
  *
  * @param Server $server
  *
  * @return Command
  */
 private function createCommand(Server $server)
 {
     $cmd = ['count' => $this->collectionName];
     if (!empty($this->filter)) {
         $cmd['query'] = (object) $this->filter;
     }
     foreach (['hint', 'limit', 'maxTimeMS', 'skip'] as $option) {
         if (isset($this->options[$option])) {
             $cmd[$option] = $this->options[$option];
         }
     }
     if (isset($this->options['readConcern']) && Functions::serverSupportsFeature($server, self::$wireVersionForReadConcern)) {
         $cmd['readConcern'] = Functions::readConcernAsDocument($this->options['readConcern']);
     }
     return new Command($cmd);
 }
Example #7
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return UpdateResult
  */
 public function execute(Server $server)
 {
     $updateOptions = ['multi' => $this->options['multi'], 'upsert' => $this->options['upsert']];
     $bulkOptions = [];
     if (isset($this->options['bypassDocumentValidation']) && Functions::serverSupportsFeature($server, self::$wireVersionForDocumentLevelValidation)) {
         $bulkOptions['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
     }
     $bulk = new Bulk($bulkOptions);
     $bulk->update($this->filter, $this->update, $updateOptions);
     $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
     $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
     return new UpdateResult($writeResult);
 }
Example #8
0
 /**
  * Execute the operation.
  *
  * @see Executable::execute()
  *
  * @param Server $server
  *
  * @return BulkWriteResult
  */
 public function execute(Server $server)
 {
     $options = ['ordered' => $this->options['ordered']];
     if (isset($this->options['bypassDocumentValidation']) && Functions::serverSupportsFeature($server, self::$wireVersionForDocumentLevelValidation)) {
         $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
     }
     $bulk = new Bulk($options);
     $insertedIds = [];
     foreach ($this->operations as $i => $operation) {
         $type = key($operation);
         $args = current($operation);
         switch ($type) {
             case self::DELETE_MANY:
             case self::DELETE_ONE:
                 $bulk->delete($args[0], $args[1]);
                 break;
             case self::INSERT_ONE:
                 $insertedId = $bulk->insert($args[0]);
                 if ($insertedId !== null) {
                     $insertedIds[$i] = $insertedId;
                 } else {
                     $insertedIds[$i] = Functions::extractIdFromInsertedDocument($args[0]);
                 }
                 break;
             case self::REPLACE_ONE:
             case self::UPDATE_MANY:
             case self::UPDATE_ONE:
                 $bulk->update($args[0], $args[1], $args[2]);
         }
     }
     $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null;
     $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern);
     return new BulkWriteResult($writeResult, $insertedIds);
 }
Example #9
0
 /**
  * Create the aggregate command.
  *
  * @param Server  $server
  * @param boolean $isCursorSupported
  *
  * @return Command
  */
 private function createCommand(Server $server, $isCursorSupported)
 {
     $cmd = ['aggregate' => $this->collectionName, 'pipeline' => $this->pipeline];
     // Servers < 2.6 do not support any command options
     if (!$isCursorSupported) {
         return new Command($cmd);
     }
     $cmd['allowDiskUse'] = $this->options['allowDiskUse'];
     if (isset($this->options['bypassDocumentValidation']) && Functions::serverSupportsFeature($server, self::$wireVersionForDocumentLevelValidation)) {
         $cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
     }
     if (isset($this->options['maxTimeMS'])) {
         $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
     }
     if (isset($this->options['readConcern']) && Functions::serverSupportsFeature($server, self::$wireVersionForReadConcern)) {
         $cmd['readConcern'] = Functions::readConcernAsDocument($this->options['readConcern']);
     }
     if ($this->options['useCursor']) {
         $cmd['cursor'] = isset($this->options["batchSize"]) ? ['batchSize' => $this->options["batchSize"]] : new stdClass();
     }
     return new Command($cmd);
 }
Example #10
0
 /**
  * Create the findAndModify command.
  *
  * @param Server $server
  *
  * @return Command
  */
 private function createCommand(Server $server)
 {
     $cmd = ['findAndModify' => $this->collectionName];
     if ($this->options['remove']) {
         $cmd['remove'] = true;
     } else {
         $cmd['new'] = $this->options['new'];
         $cmd['upsert'] = $this->options['upsert'];
     }
     foreach (['fields', 'query', 'sort', 'update'] as $option) {
         if (isset($this->options[$option])) {
             $cmd[$option] = (object) $this->options[$option];
         }
     }
     if (isset($this->options['maxTimeMS'])) {
         $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
     }
     if (isset($this->options['bypassDocumentValidation']) && Functions::serverSupportsFeature($server, self::$wireVersionForDocumentLevelValidation)) {
         $cmd['bypassDocumentValidation'] = $this->options['bypassDocumentValidation'];
     }
     if (isset($this->options['writeConcern']) && Functions::serverSupportsFeature($server, self::$wireVersionForWriteConcern)) {
         $cmd['writeConcern'] = $this->options['writeConcern'];
     }
     return new Command($cmd);
 }