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;
  19  
  20  use MongoDB\Driver\WriteResult;
  21  use MongoDB\Exception\BadMethodCallException;
  22  
  23  /**
  24   * Result class for a bulk write operation.
  25   */
  26  class BulkWriteResult
  27  {
  28      /** @var WriteResult */
  29      private $writeResult;
  30  
  31      /** @var array */
  32      private $insertedIds;
  33  
  34      /** @var boolean */
  35      private $isAcknowledged;
  36  
  37      public function __construct(WriteResult $writeResult, array $insertedIds)
  38      {
  39          $this->writeResult = $writeResult;
  40          $this->insertedIds = $insertedIds;
  41          $this->isAcknowledged = $writeResult->isAcknowledged();
  42      }
  43  
  44      /**
  45       * Return the number of documents that were deleted.
  46       *
  47       * This method should only be called if the write was acknowledged.
  48       *
  49       * @see BulkWriteResult::isAcknowledged()
  50       * @return integer|null
  51       * @throws BadMethodCallException is the write result is unacknowledged
  52       */
  53      public function getDeletedCount()
  54      {
  55          if ($this->isAcknowledged) {
  56              return $this->writeResult->getDeletedCount();
  57          }
  58  
  59          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
  60      }
  61  
  62      /**
  63       * Return the number of documents that were inserted.
  64       *
  65       * This method should only be called if the write was acknowledged.
  66       *
  67       * @see BulkWriteResult::isAcknowledged()
  68       * @return integer|null
  69       * @throws BadMethodCallException is the write result is unacknowledged
  70       */
  71      public function getInsertedCount()
  72      {
  73          if ($this->isAcknowledged) {
  74              return $this->writeResult->getInsertedCount();
  75          }
  76  
  77          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
  78      }
  79  
  80      /**
  81       * Return a map of the inserted documents' IDs.
  82       *
  83       * The index of each ID in the map corresponds to each document's position
  84       * in the bulk operation. If a document had an ID prior to inserting (i.e.
  85       * the driver did not generate an ID), the index will contain its "_id"
  86       * field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
  87       * instance.
  88       *
  89       * @return array
  90       */
  91      public function getInsertedIds()
  92      {
  93          return $this->insertedIds;
  94      }
  95  
  96      /**
  97       * Return the number of documents that were matched by the filter.
  98       *
  99       * This method should only be called if the write was acknowledged.
 100       *
 101       * @see BulkWriteResult::isAcknowledged()
 102       * @return integer|null
 103       * @throws BadMethodCallException is the write result is unacknowledged
 104       */
 105      public function getMatchedCount()
 106      {
 107          if ($this->isAcknowledged) {
 108              return $this->writeResult->getMatchedCount();
 109          }
 110  
 111          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
 112      }
 113  
 114      /**
 115       * Return the number of documents that were modified.
 116       *
 117       * This value is undefined (i.e. null) if the write executed as a legacy
 118       * operation instead of command.
 119       *
 120       * This method should only be called if the write was acknowledged.
 121       *
 122       * @see BulkWriteResult::isAcknowledged()
 123       * @return integer|null
 124       * @throws BadMethodCallException is the write result is unacknowledged
 125       */
 126      public function getModifiedCount()
 127      {
 128          if ($this->isAcknowledged) {
 129              return $this->writeResult->getModifiedCount();
 130          }
 131  
 132          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
 133      }
 134  
 135      /**
 136       * Return the number of documents that were upserted.
 137       *
 138       * This method should only be called if the write was acknowledged.
 139       *
 140       * @see BulkWriteResult::isAcknowledged()
 141       * @return integer|null
 142       * @throws BadMethodCallException is the write result is unacknowledged
 143       */
 144      public function getUpsertedCount()
 145      {
 146          if ($this->isAcknowledged) {
 147              return $this->writeResult->getUpsertedCount();
 148          }
 149  
 150          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
 151      }
 152  
 153      /**
 154       * Return a map of the upserted documents' IDs.
 155       *
 156       * The index of each ID in the map corresponds to each document's position
 157       * in bulk operation. If a document had an ID prior to upserting (i.e. the
 158       * server did not need to generate an ID), this will contain its "_id". Any
 159       * server-generated ID will be a MongoDB\BSON\ObjectId instance.
 160       *
 161       * This method should only be called if the write was acknowledged.
 162       *
 163       * @see BulkWriteResult::isAcknowledged()
 164       * @return array
 165       * @throws BadMethodCallException is the write result is unacknowledged
 166       */
 167      public function getUpsertedIds()
 168      {
 169          if ($this->isAcknowledged) {
 170              return $this->writeResult->getUpsertedIds();
 171          }
 172  
 173          throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
 174      }
 175  
 176      /**
 177       * Return whether this update was acknowledged by the server.
 178       *
 179       * If the update was not acknowledged, other fields from the WriteResult
 180       * (e.g. matchedCount) will be undefined.
 181       *
 182       * @return boolean
 183       */
 184      public function isAcknowledged()
 185      {
 186          return $this->isAcknowledged;
 187      }
 188  }