Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401]

   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 current;
  25  
  26  /**
  27   * Operation for finding a single document with the find command.
  28   *
  29   * @api
  30   * @see \MongoDB\Collection::findOne()
  31   * @see http://docs.mongodb.org/manual/tutorial/query-documents/
  32   * @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
  33   */
  34  class FindOne implements Executable, Explainable
  35  {
  36      /** @var Find */
  37      private $find;
  38  
  39      /**
  40       * Constructs a find command for finding a single 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       *  * comment (string): Attaches a comment to the query. If "$comment" also
  50       *    exists in the modifiers document, this option will take precedence.
  51       *
  52       *  * hint (string|document): The index to use. Specify either the index
  53       *    name as a string or the index key pattern as a document. If specified,
  54       *    then the query system will only consider plans using the hinted index.
  55       *
  56       *  * max (document): The exclusive upper bound for a specific index.
  57       *
  58       *  * maxScan (integer): Maximum number of documents or index keys to scan
  59       *    when executing the query.
  60       *
  61       *    This option has been deprecated since version 1.4.
  62       *
  63       *  * maxTimeMS (integer): The maximum amount of time to allow the query to
  64       *    run. If "$maxTimeMS" also exists in the modifiers document, this
  65       *    option will take precedence.
  66       *
  67       *  * min (document): The inclusive upper bound for a specific index.
  68       *
  69       *  * modifiers (document): Meta-operators modifying the output or behavior
  70       *    of a query.
  71       *
  72       *  * projection (document): Limits the fields to return for the matching
  73       *    document.
  74       *
  75       *  * readConcern (MongoDB\Driver\ReadConcern): Read concern.
  76       *
  77       *    This is not supported for server versions < 3.2 and will result in an
  78       *    exception at execution time if used.
  79       *
  80       *  * readPreference (MongoDB\Driver\ReadPreference): Read preference.
  81       *
  82       *  * returnKey (boolean): If true, returns only the index keys in the
  83       *    resulting documents.
  84       *
  85       *  * session (MongoDB\Driver\Session): Client session.
  86       *
  87       *    Sessions are not supported for server versions < 3.6.
  88       *
  89       *  * showRecordId (boolean): Determines whether to return the record
  90       *    identifier for each document. If true, adds a field $recordId to the
  91       *    returned documents.
  92       *
  93       *  * skip (integer): The number of documents to skip before returning.
  94       *
  95       *  * sort (document): The order in which to return matching documents. If
  96       *    "$orderby" also exists in the modifiers document, this option will
  97       *    take precedence.
  98       *
  99       *  * typeMap (array): Type map for BSON deserialization.
 100       *
 101       * @param string       $databaseName   Database name
 102       * @param string       $collectionName Collection name
 103       * @param array|object $filter         Query by which to filter documents
 104       * @param array        $options        Command options
 105       * @throws InvalidArgumentException for parameter/option parsing errors
 106       */
 107      public function __construct($databaseName, $collectionName, $filter, array $options = [])
 108      {
 109          $this->find = new Find(
 110              $databaseName,
 111              $collectionName,
 112              $filter,
 113              ['limit' => 1] + $options
 114          );
 115      }
 116  
 117      /**
 118       * Execute the operation.
 119       *
 120       * @see Executable::execute()
 121       * @param Server $server
 122       * @return array|object|null
 123       * @throws UnsupportedException if collation or read concern is used and unsupported
 124       * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
 125       */
 126      public function execute(Server $server)
 127      {
 128          $cursor = $this->find->execute($server);
 129          $document = current($cursor->toArray());
 130  
 131          return $document === false ? null : $document;
 132      }
 133  
 134      public function getCommandDocument(Server $server)
 135      {
 136          return $this->find->getCommandDocument($server);
 137      }
 138  }