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 updating multiple documents with the update command.
  32   *
  33   * @api
  34   * @see \MongoDB\Collection::updateMany()
  35   * @see http://docs.mongodb.org/manual/reference/command/update/
  36   */
  37  class UpdateMany implements Executable, Explainable
  38  {
  39      /** @var Update */
  40      private $update;
  41  
  42      /**
  43       * Constructs an update command.
  44       *
  45       * Supported options:
  46       *
  47       *  * arrayFilters (document array): A set of filters specifying to which
  48       *    array elements an update should apply.
  49       *
  50       *    This is not supported for server versions < 3.6 and will result in an$
  51       *    exception at execution time if used.
  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.2 and will result in an
  69       *    exception at execution time if used.
  70       *
  71       *  * session (MongoDB\Driver\Session): Client session.
  72       *
  73       *    Sessions are not supported for server versions < 3.6.
  74       *
  75       *  * upsert (boolean): When true, a new document is created if no document
  76       *    matches the query. The default is false.
  77       *
  78       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  79       *
  80       * @param string       $databaseName   Database name
  81       * @param string       $collectionName Collection name
  82       * @param array|object $filter         Query by which to filter documents
  83       * @param array|object $update         Update to apply to the matched documents
  84       * @param array        $options        Command options
  85       * @throws InvalidArgumentException for parameter/option parsing errors
  86       */
  87      public function __construct($databaseName, $collectionName, $filter, $update, array $options = [])
  88      {
  89          if (! is_array($update) && ! is_object($update)) {
  90              throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
  91          }
  92  
  93          if (! is_first_key_operator($update) && ! is_pipeline($update)) {
  94              throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
  95          }
  96  
  97          $this->update = new Update(
  98              $databaseName,
  99              $collectionName,
 100              $filter,
 101              $update,
 102              ['multi' => true] + $options
 103          );
 104      }
 105  
 106      /**
 107       * Execute the operation.
 108       *
 109       * @see Executable::execute()
 110       * @param Server $server
 111       * @return UpdateResult
 112       * @throws UnsupportedException if collation is used and unsupported
 113       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 114       */
 115      public function execute(Server $server)
 116      {
 117          return $this->update->execute($server);
 118      }
 119  
 120      public function getCommandDocument(Server $server)
 121      {
 122          return $this->update->getCommandDocument($server);
 123      }
 124  }