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