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  
  25  use function is_array;
  26  use function is_object;
  27  
  28  /**
  29   * Operation for deleting a document with the findAndModify command.
  30   *
  31   * @api
  32   * @see \MongoDB\Collection::findOneAndDelete()
  33   * @see https://mongodb.com/docs/manual/reference/command/findAndModify/
  34   */
  35  class FindOneAndDelete implements Executable, Explainable
  36  {
  37      /** @var FindAndModify */
  38      private $findAndModify;
  39  
  40      /**
  41       * Constructs a findAndModify command for deleting a document.
  42       *
  43       * Supported options:
  44       *
  45       *  * collation (document): Collation specification.
  46       *
  47       *  * comment (mixed): BSON value to attach as a comment to this command.
  48       *
  49       *    This is not supported for servers versions < 4.4.
  50       *
  51       *  * hint (string|document): The index to use. Specify either the index
  52       *    name as a string or the index key pattern as a document. If specified,
  53       *    then the query system will only consider plans using the hinted index.
  54       *
  55       *    This is not supported for server versions < 4.4 and will result in an
  56       *    exception at execution time if used.
  57       *
  58       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  59       *    run.
  60       *
  61       *  * projection (document): Limits the fields to return for the matching
  62       *    document.
  63       *
  64       *  * session (MongoDB\Driver\Session): Client session.
  65       *
  66       *  * sort (document): Determines which document the operation modifies if
  67       *    the query selects multiple documents.
  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       *  * typeMap (array): Type map for BSON deserialization.
  75       *
  76       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  77       *
  78       * @param string       $databaseName   Database name
  79       * @param string       $collectionName Collection name
  80       * @param array|object $filter         Query by which to filter documents
  81       * @param array        $options        Command options
  82       * @throws InvalidArgumentException for parameter/option parsing errors
  83       */
  84      public function __construct(string $databaseName, string $collectionName, $filter, array $options = [])
  85      {
  86          if (! is_array($filter) && ! is_object($filter)) {
  87              throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
  88          }
  89  
  90          if (isset($options['projection']) && ! is_array($options['projection']) && ! is_object($options['projection'])) {
  91              throw InvalidArgumentException::invalidType('"projection" option', $options['projection'], 'array or object');
  92          }
  93  
  94          if (isset($options['projection'])) {
  95              $options['fields'] = $options['projection'];
  96          }
  97  
  98          unset($options['projection']);
  99  
 100          $this->findAndModify = new FindAndModify(
 101              $databaseName,
 102              $collectionName,
 103              ['query' => $filter, 'remove' => true] + $options
 104          );
 105      }
 106  
 107      /**
 108       * Execute the operation.
 109       *
 110       * @see Executable::execute()
 111       * @return array|object|null
 112       * @throws UnsupportedException if collation or write concern 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->findAndModify->execute($server);
 118      }
 119  
 120      /**
 121       * Returns the command document for this operation.
 122       *
 123       * @see Explainable::getCommandDocument()
 124       * @return array
 125       */
 126      public function getCommandDocument(Server $server)
 127      {
 128          return $this->findAndModify->getCommandDocument($server);
 129      }
 130  }