FindOne.php 4.91 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2015-2017 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17 18 19

namespace MongoDB\Operation;

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 28 29

/**
 * Operation for finding a single document with the find command.
 *
 * @api
30
 * @see \MongoDB\Collection::findOne()
31 32 33
 * @see http://docs.mongodb.org/manual/tutorial/query-documents/
 * @see http://docs.mongodb.org/manual/reference/operator/query-modifier/
 */
34
class FindOne implements Executable, Explainable
35
{
36
    /** @var Find */
37 38 39 40 41 42 43
    private $find;

    /**
     * Constructs a find command for finding a single document.
     *
     * Supported options:
     *
44 45 46 47 48
     *  * collation (document): Collation specification.
     *
     *    This is not supported for server versions < 3.4 and will result in an
     *    exception at execution time if used.
     *
49 50 51
     *  * comment (string): Attaches a comment to the query. If "$comment" also
     *    exists in the modifiers document, this option will take precedence.
     *
52 53 54 55
     *  * hint (string|document): The index to use. Specify either the index
     *    name as a string or the index key pattern as a document. If specified,
     *    then the query system will only consider plans using the hinted index.
     *
56 57 58 59 60
     *  * max (document): The exclusive upper bound for a specific index.
     *
     *  * maxScan (integer): Maximum number of documents or index keys to scan
     *    when executing the query.
     *
61 62
     *    This option has been deprecated since version 1.4.
     *
63 64 65 66
     *  * maxTimeMS (integer): The maximum amount of time to allow the query to
     *    run. If "$maxTimeMS" also exists in the modifiers document, this
     *    option will take precedence.
     *
67 68
     *  * min (document): The inclusive upper bound for a specific index.
     *
69 70 71 72 73 74
     *  * modifiers (document): Meta-operators modifying the output or behavior
     *    of a query.
     *
     *  * projection (document): Limits the fields to return for the matching
     *    document.
     *
75 76
     *  * readConcern (MongoDB\Driver\ReadConcern): Read concern.
     *
77 78
     *    This is not supported for server versions < 3.2 and will result in an
     *    exception at execution time if used.
79
     *
80 81
     *  * readPreference (MongoDB\Driver\ReadPreference): Read preference.
     *
82 83 84
     *  * returnKey (boolean): If true, returns only the index keys in the
     *    resulting documents.
     *
85 86 87 88
     *  * session (MongoDB\Driver\Session): Client session.
     *
     *    Sessions are not supported for server versions < 3.6.
     *
89 90 91 92
     *  * showRecordId (boolean): Determines whether to return the record
     *    identifier for each document. If true, adds a field $recordId to the
     *    returned documents.
     *
93 94 95 96 97 98
     *  * skip (integer): The number of documents to skip before returning.
     *
     *  * sort (document): The order in which to return matching documents. If
     *    "$orderby" also exists in the modifiers document, this option will
     *    take precedence.
     *
99 100
     *  * typeMap (array): Type map for BSON deserialization.
     *
101 102 103 104
     * @param string       $databaseName   Database name
     * @param string       $collectionName Collection name
     * @param array|object $filter         Query by which to filter documents
     * @param array        $options        Command options
105
     * @throws InvalidArgumentException for parameter/option parsing errors
106
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
107
    public function __construct($databaseName, $collectionName, $filter, array $options = [])
108 109 110 111 112
    {
        $this->find = new Find(
            $databaseName,
            $collectionName,
            $filter,
113
            ['limit' => 1] + $options
114 115 116 117 118 119 120 121
        );
    }

    /**
     * Execute the operation.
     *
     * @see Executable::execute()
     * @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 127 128 129 130
     */
    public function execute(Server $server)
    {
        $cursor = $this->find->execute($server);
        $document = current($cursor->toArray());

131
        return $document === false ? null : $document;
132
    }
133

134
    public function getCommandDocument(Server $server)
135
    {
136
        return $this->find->getCommandDocument($server);
137
    }
138
}