Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   1  <?php
   2  /*
   3   * Copyright 2015-present 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   *   https://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  
  26  use function is_array;
  27  use function is_object;
  28  use function MongoDB\is_first_key_operator;
  29  use function MongoDB\is_pipeline;
  30  
  31  /**
  32   * Operation for replacing a single document with the update command.
  33   *
  34   * @api
  35   * @see \MongoDB\Collection::replaceOne()
  36   * @see https://mongodb.com/docs/manual/reference/command/update/
  37   */
  38  class ReplaceOne implements Executable
  39  {
  40      /** @var Update */
  41      private $update;
  42  
  43      /**
  44       * Constructs an update command.
  45       *
  46       * Supported options:
  47       *
  48       *  * bypassDocumentValidation (boolean): If true, allows the write to
  49       *    circumvent document level validation.
  50       *
  51       *  * collation (document): Collation specification.
  52       *
  53       *  * comment (mixed): BSON value to attach as a comment to this command.
  54       *
  55       *    This is not supported for servers versions < 4.4.
  56       *
  57       *  * hint (string|document): The index to use. Specify either the index
  58       *    name as a string or the index key pattern as a document. If specified,
  59       *    then the query system will only consider plans using the hinted index.
  60       *
  61       *    This is not supported for server versions < 4.2 and will result in an
  62       *    exception at execution time if used.
  63       *
  64       *  * session (MongoDB\Driver\Session): Client session.
  65       *
  66       *  * upsert (boolean): When true, a new document is created if no document
  67       *    matches the query. The default is false.
  68       *
  69       *  * let (document): Map of parameter names and values. Values must be
  70       *    constant or closed expressions that do not reference document fields.
  71       *    Parameters can then be accessed as variables in an aggregate
  72       *    expression context (e.g. "$$var").
  73       *
  74       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  75       *
  76       * @param string       $databaseName   Database name
  77       * @param string       $collectionName Collection name
  78       * @param array|object $filter         Query by which to filter documents
  79       * @param array|object $replacement    Replacement document
  80       * @param array        $options        Command options
  81       * @throws InvalidArgumentException for parameter/option parsing errors
  82       */
  83      public function __construct(string $databaseName, string $collectionName, $filter, $replacement, array $options = [])
  84      {
  85          if (! is_array($replacement) && ! is_object($replacement)) {
  86              throw InvalidArgumentException::invalidType('$replacement', $replacement, 'array or object');
  87          }
  88  
  89          if (is_first_key_operator($replacement)) {
  90              throw new InvalidArgumentException('First key in $replacement argument is an update operator');
  91          }
  92  
  93          if (is_pipeline($replacement)) {
  94              throw new InvalidArgumentException('$replacement argument is a pipeline');
  95          }
  96  
  97          $this->update = new Update(
  98              $databaseName,
  99              $collectionName,
 100              $filter,
 101              $replacement,
 102              ['multi' => false] + $options
 103          );
 104      }
 105  
 106      /**
 107       * Execute the operation.
 108       *
 109       * @see Executable::execute()
 110       * @return UpdateResult
 111       * @throws UnsupportedException if collation is used and unsupported
 112       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 113       */
 114      public function execute(Server $server)
 115      {
 116          return $this->update->execute($server);
 117      }
 118  }