- Reference >
mongoshMethods >- Collection Methods >
- db.collection.findOne()
db.collection.findOne()¶
On this page
Definition¶
-
db.collection.findOne(query, projection)¶ Important
mongoshMethodThis page documents a
mongoshmethod. This is not the documentation for a language-specific driver such as Node.js.For MongoDB API drivers, refer to the language-specific :driver:`MongoDB driver documentation </>`.
For the legacy
mongoshell documentation, refer to the documentation for the corresponding MongoDB Server release:Returns one document that satisfies the specified query criteria on the collection or view. If multiple documents satisfy the query, this method returns the first document according to the natural order which reflects the order of documents on the disk. In capped collections, natural order is the same as insertion order. If no document satisfies the query, the method returns null.
Parameter Type Description querydocument Optional. Specifies query selection criteria using query operators. projectiondocument Optional. Specifies the fields to return using projection operators. Omit this parameter to return all fields in the matching document. For details, see Projection. Returns: One document that satisfies the criteria specified as the first argument to this method. If you specify a projectionparameter,findOne()returns a document that only contains theprojectionfields. The_idfield is always included unless you explicitly exclude it.Although similar to the
find()method, thefindOne()method returns a document rather than a cursor.
Behavior¶
Client Disconnection¶
Starting in MongoDB 4.2, if the client that issued db.collection.findOne()
disconnects before the operation completes, MongoDB marks db.collection.findOne()
for termination using killOp.
Projection¶
Important
Language Consistency
Starting in MongoDB 4.4, as part of making
find and
findAndModify projection consistent with
aggregation’s $project stage,
- The
findandfindAndModifyprojection can accept aggregation expressions and syntax. - MongoDB enforces additional restrictions with regards to
projections. See
Projection Restrictionsfor details.
The projection parameter determines which fields are returned in
the matching documents. The projection parameter takes a document
of the following form:
| Projection | Description |
|---|---|
<field>: <1 or true> |
Specifies the inclusion of a field. Non-zero integers are also
treated as true. |
<field>: <0 or false> |
Specifies the exclusion of a field. |
"<field>.$": <1 or true> |
With the use of the $ array projection operator,
you can specify the projection to return the first element
that match the query condition on the array field; e.g.
"arrayField.$" : 1. (Not available for views.) Non-zero integers are also treated as
true. |
<field>: <array projection> |
Using the array projection operators $elemMatch,
$slice, specifies the array element(s) to include,
thereby excluding those elements that do not meet the
expressions. (Not available for views.) |
<field>: <$meta expression> |
Using the $meta operator expression, specifies the
inclusion of available per-document metadata. (Not available for views.) |
<field>: <aggregation expression> |
Specifies the value of the projected field. Starting in MongoDB 4.4, with the use of aggregation expressions and syntax, including the use of literals and aggregation variables, you can project new fields or project existing fields with new values. For example,
In versions 4.2 and earlier, any specification value (with
the exception of the previously unsupported document
value) is treated as either New in version 4.4. |
Embedded Field Specification¶
For fields in an embedded documents, you can specify the field using either:
- dot notation; e.g.
"field.nestedfield": <value> - nested form; e.g.
{ field: { nestedfield: <value> } }(Starting in MongoDB 4.4)
_id Field Projection¶
The _id field is included in the returned documents by default unless
you explicitly specify _id: 0 in the projection to suppress the field.
Inclusion or Exclusion¶
A projection cannot contain both include and exclude
specifications, with the exception of the _id field:
- In projections that explicitly include fields, the
_idfield is the only field that you can explicitly exclude. - In projections that explicitly excludes fields, the
_idfield is the only field that you can explicitly include; however, the_idfield is included by default.
For more information on projection, see also:
Examples¶
With Empty Query Specification¶
The following operation returns a single document from the bios collection:
With a Query Specification¶
The following operation returns the first matching document from the
bios collection where either
the field first in the embedded document name starts with the letter
G or where the field birth is less than new
Date('01/01/1945'):
With a Projection¶
The projection parameter specifies which fields to return. The
parameter contains either include or exclude specifications, not both,
unless the exclude is for the _id field.
Specify the Fields to Return¶
The following operation finds a document in the bios collection and returns only the name,
contribs and _id fields:
Return All but the Excluded Fields¶
The following operation returns a document in the bios collection where the contribs field
contains the element OOP and returns all fields except the _id
field, the first field in the name embedded document, and the
birth field: