Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 39 and 311]

   1  <?php
   2  /*
   3   * Copyright 2015-2017 MongoDB, Inc.
   4   *
   5   * Licensed under the Apache License, Version 2.0 (the "License");
   6   * you may not use this file except in compliance with the License.
   7   * You may obtain a copy of the License at
   8   *
   9   *   http://www.apache.org/licenses/LICENSE-2.0
  10   *
  11   * Unless required by applicable law or agreed to in writing, software
  12   * distributed under the License is distributed on an "AS IS" BASIS,
  13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14   * See the License for the specific language governing permissions and
  15   * limitations under the License.
  16   */
  17  
  18  namespace MongoDB\Operation;
  19  
  20  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  21  use MongoDB\Driver\Server;
  22  use MongoDB\Exception\InvalidArgumentException;
  23  use MongoDB\Exception\UnsupportedException;
  24  use MongoDB\UpdateResult;
  25  use function is_array;
  26  use function is_object;
  27  use function MongoDB\is_first_key_operator;
  28  use function MongoDB\is_pipeline;
  29  
  30  /**
  31   * Operation for replacing a single document with the update command.
  32   *
  33   * @api
  34   * @see \MongoDB\Collection::replaceOne()
  35   * @see http://docs.mongodb.org/manual/reference/command/update/
  36   */
  37  class ReplaceOne implements Executable
  38  {
  39      /** @var Update */
  40      private $update;
  41  
  42      /**
  43       * Constructs an update command.
  44       *
  45       * Supported options:
  46       *
  47       *  * bypassDocumentValidation (boolean): If true, allows the write to
  48       *    circumvent document level validation.
  49       *
  50       *    For servers < 3.2, this option is ignored as document level validation
  51       *    is not available.
  52       *
  53       *  * collation (document): Collation specification.
  54       *
  55       *    This is not supported for server versions < 3.4 and will result in an
  56       *    exception at execution time if used.
  57       *
  58       *  * hint (string|document): The index to use. Specify either the index
  59       *    name as a string or the index key pattern as a document. If specified,
  60       *    then the query system will only consider plans using the hinted index.
  61       *
  62       *    This is not supported for server versions < 4.2 and will result in an
  63       *    exception at execution time if used.
  64       *
  65       *  * session (MongoDB\Driver\Session): Client session.
  66       *
  67       *    Sessions are not supported for server versions < 3.6.
  68       *
  69       *  * upsert (boolean): When true, a new document is created if no document
  70       *    matches the query. The default is false.
  71       *
  72       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  73       *
  74       * @param string       $databaseName   Database name
  75       * @param string       $collectionName Collection name
  76       * @param array|object $filter         Query by which to filter documents
  77       * @param array|object $replacement    Replacement document
  78       * @param array        $options        Command options
  79       * @throws InvalidArgumentException for parameter/option parsing errors
  80       */
  81      public function __construct($databaseName, $collectionName, $filter, $replacement, array $options = [])
  82      {
  83          if (! is_array($replacement) && ! is_object($replacement)) {
  84              throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object');
  85          }
  86  
  87          if (is_first_key_operator($replacement)) {
  88              throw new InvalidArgumentException('First key in $replacement argument is an update operator');
  89          }
  90  
  91          if (is_pipeline($replacement)) {
  92              throw new InvalidArgumentException('$replacement argument is a pipeline');
  93          }
  94  
  95          $this->update = new Update(
  96              $databaseName,
  97              $collectionName,
  98              $filter,
  99              $replacement,
 100              ['multi' => false] + $options
 101          );
 102      }
 103  
 104      /**
 105       * Execute the operation.
 106       *
 107       * @see Executable::execute()
 108       * @param Server $server
 109       * @return UpdateResult
 110       * @throws UnsupportedException if collation is used and unsupported
 111       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 112       */
 113      public function execute(Server $server)
 114      {
 115          return $this->update->execute($server);
 116      }
 117  }