findOne( query, projection )
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
document Optional. Specifies query selection criteria using query operators.
document Optional. Specifies the fields to return using projection operators. Omit this parameter to return all fields in the matching document.
projectionparameter takes a document of the following form:
<boolean>can be one of the following include or exclude values:
trueto include. The
findOne()method always includes the _id field even if the field is not explicitly specified in the projection parameter.
The projection argument cannot mix include and exclude specifications, with the exception of excluding the
Returns: One document that satisfies the criteria specified as the first argument to this method. If you specify a
findOne()returns a document that only contains the
_idfield is always included unless you explicitly exclude it.
The following operation returns a single document from the bios collection:
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
projection parameter specifies which fields to return. The parameter contains either include or exclude specifications, not both, unless the exclude is for the
The following operation finds a document in the bios collection and returns only the
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
You cannot apply cursor methods to the result of
findOne() because a single document is returned. You have access to the document directly: