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 function is_array;
  25  use function is_integer;
  26  use function is_object;
  27  use function MongoDB\is_first_key_operator;
  28  use function MongoDB\is_pipeline;
  29  
  30  /**
  31   * Operation for updating a document with the findAndModify command.
  32   *
  33   * @api
  34   * @see \MongoDB\Collection::findOneAndUpdate()
  35   * @see http://docs.mongodb.org/manual/reference/command/findAndModify/
  36   */
  37  class FindOneAndUpdate implements Executable, Explainable
  38  {
  39      const RETURN_DOCUMENT_BEFORE = 1;
  40      const RETURN_DOCUMENT_AFTER = 2;
  41  
  42      /** @var FindAndModify */
  43      private $findAndModify;
  44  
  45      /**
  46       * Constructs a findAndModify command for updating a document.
  47       *
  48       * Supported options:
  49       *
  50       *  * arrayFilters (document array): A set of filters specifying to which
  51       *    array elements an update should apply.
  52       *
  53       *  * bypassDocumentValidation (boolean): If true, allows the write to
  54       *    circumvent document level validation.
  55       *
  56       *    For servers < 3.2, this option is ignored as document level validation
  57       *    is not available.
  58       *
  59       *  * collation (document): Collation specification.
  60       *
  61       *    This is not supported for server versions < 3.4 and will result in an
  62       *    exception at execution time if used.
  63       *
  64       *  * hint (string|document): The index to use. Specify either the index
  65       *    name as a string or the index key pattern as a document. If specified,
  66       *    then the query system will only consider plans using the hinted index.
  67       *
  68       *    This is not supported for server versions < 4.4 and will result in an
  69       *    exception at execution time if used.
  70       *
  71       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  72       *    run.
  73       *
  74       *  * projection (document): Limits the fields to return for the matching
  75       *    document.
  76       *
  77       *  * returnDocument (enum): Whether to return the document before or after
  78       *    the update is applied. Must be either
  79       *    FindOneAndUpdate::RETURN_DOCUMENT_BEFORE or
  80       *    FindOneAndUpdate::RETURN_DOCUMENT_AFTER. The default is
  81       *    FindOneAndUpdate::RETURN_DOCUMENT_BEFORE.
  82       *
  83       *  * session (MongoDB\Driver\Session): Client session.
  84       *
  85       *    Sessions are not supported for server versions < 3.6.
  86       *
  87       *  * sort (document): Determines which document the operation modifies if
  88       *    the query selects multiple documents.
  89       *
  90       *  * typeMap (array): Type map for BSON deserialization.
  91       *
  92       *  * upsert (boolean): When true, a new document is created if no document
  93       *    matches the query. The default is false.
  94       *
  95       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  96       *
  97       *    This is not supported for server versions < 3.2 and will result in an
  98       *    exception at execution time if used.
  99       *
 100       * @param string       $databaseName   Database name
 101       * @param string       $collectionName Collection name
 102       * @param array|object $filter         Query by which to filter documents
 103       * @param array|object $update         Update to apply to the matched document
 104       * @param array        $options        Command options
 105       * @throws InvalidArgumentException for parameter/option parsing errors
 106       */
 107      public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
 108      {
 109          if (! is_array($filter) && ! is_object($filter)) {
 110              throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
 111          }
 112  
 113          if (! is_array($update) && ! is_object($update)) {
 114              throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
 115          }
 116  
 117          if (! is_first_key_operator($update) && ! is_pipeline($update)) {
 118              throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
 119          }
 120  
 121          $options += [
 122              'returnDocument' => self::RETURN_DOCUMENT_BEFORE,
 123              'upsert' => false,
 124          ];
 125  
 126          if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
 127              throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
 128          }
 129  
 130          if (! is_integer($options['returnDocument'])) {
 131              throw InvalidArgumentException::invalidType('"returnDocument" option', $options['returnDocument'], 'integer');
 132          }
 133  
 134          if ($options['returnDocument'] !== self::RETURN_DOCUMENT_AFTER &&
 135              $options['returnDocument'] !== self::RETURN_DOCUMENT_BEFORE) {
 136              throw new InvalidArgumentException('Invalid value for "returnDocument" option: ' . $options['returnDocument']);
 137          }
 138  
 139          if (isset($options['projection'])) {
 140              $options['fields'] = $options['projection'];
 141          }
 142  
 143          $options['new'] = $options['returnDocument'] === self::RETURN_DOCUMENT_AFTER;
 144  
 145          unset($options['projection'], $options['returnDocument']);
 146  
 147          $this->findAndModify = new FindAndModify(
 148              $databaseName,
 149              $collectionName,
 150              ['query' => $filter, 'update' => $update] + $options
 151          );
 152      }
 153  
 154      /**
 155       * Execute the operation.
 156       *
 157       * @see Executable::execute()
 158       * @param Server $server
 159       * @return array|object|null
 160       * @throws UnsupportedException if collation or write concern is used and unsupported
 161       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 162       */
 163      public function execute(Server $server)
 164      {
 165          return $this->findAndModify->execute($server);
 166      }
 167  
 168      public function getCommandDocument(Server $server)
 169      {
 170          return $this->findAndModify->getCommandDocument($server);
 171      }
 172  }