- Reference >
- JavaScript Methods >
- db.collection.findOne()
db.collection.findOne()¶
-
db.collection.
findOne
(query, projection)¶ Parameters: - query (document) – Optional. A document that specifies the query using the JSON-like syntax and query operators.
- projection (document) –
Optional. Controls the fields to return, or the projection. The
projection
argument will resemble the following prototype:The
boolean
can take the following include or exclude values:1
ortrue
to include. ThefindOne()
method always includes the _id field even if the field is not explicitly stated to return in the projection parameter.0
orfalse
to exclude.
The
projection
cannot contain both include and exclude specifications except for the exclusion of the_id
field.Omit the
projection
parameter to return all the fields in the matching documents.
Returns: One document that satisfies the query specified as the argument to this method. If the
projection
argument is specified, the returned document contains only theprojection
fields, and the_id
field if you do not explicitly exclude the_id
field.Returns only one document that satisfies the specified query. 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 disc. In capped collections, natural order is the same as insertion order.