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