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\DeleteResult;
  21  use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
  22  use MongoDB\Driver\Server;
  23  use MongoDB\Exception\InvalidArgumentException;
  24  use MongoDB\Exception\UnsupportedException;
  25  
  26  /**
  27   * Operation for deleting multiple document with the delete command.
  28   *
  29   * @api
  30   * @see \MongoDB\Collection::deleteOne()
  31   * @see https://mongodb.com/docs/manual/reference/command/delete/
  32   */
  33  class DeleteMany implements Executable, Explainable
  34  {
  35      /** @var Delete */
  36      private $delete;
  37  
  38      /**
  39       * Constructs a delete command.
  40       *
  41       * Supported options:
  42       *
  43       *  * collation (document): Collation specification.
  44       *
  45       *  * comment (mixed): BSON value to attach as a comment to this command.
  46       *
  47       *    This is not supported for servers versions < 4.4.
  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       *  * let (document): Map of parameter names and values. Values must be
  57       *    constant or closed expressions that do not reference document fields.
  58       *    Parameters can then be accessed as variables in an aggregate
  59       *    expression context (e.g. "$$var").
  60       *
  61       *  * session (MongoDB\Driver\Session): Client session.
  62       *
  63       *  * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
  64       *
  65       * @param string       $databaseName   Database name
  66       * @param string       $collectionName Collection name
  67       * @param array|object $filter         Query by which to delete documents
  68       * @param array        $options        Command options
  69       * @throws InvalidArgumentException for parameter/option parsing errors
  70       */
  71      public function __construct(string $databaseName, string $collectionName, $filter, array $options = [])
  72      {
  73          $this->delete = new Delete($databaseName, $collectionName, $filter, 0, $options);
  74      }
  75  
  76      /**
  77       * Execute the operation.
  78       *
  79       * @see Executable::execute()
  80       * @return DeleteResult
  81       * @throws UnsupportedException if collation is used and unsupported
  82       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
  83       */
  84      public function execute(Server $server)
  85      {
  86          return $this->delete->execute($server);
  87      }
  88  
  89      /**
  90       * Returns the command document for this operation.
  91       *
  92       * @see Explainable::getCommandDocument()
  93       * @return array
  94       */
  95      public function getCommandDocument(Server $server)
  96      {
  97          return $this->delete->getCommandDocument($server);
  98      }
  99  }