Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js

Find a Document

Note

If you specify a callback method, findOne() returns nothing. If you do not specify one, this method returns a Promise that resolves to the result object when it completes. See our guide on Promises and Callbacks for more information, or the API documentation for information on the result object.

You can query for a single document in a collection with the collection.findOne() method. The findOne() method uses a query document that you provide to match only the subset of the documents in the collection that match the query. If you don't provide a query document or if you provide an empty document, MongoDB matches all documents in the collection. The findOne() operation only returns the first matched document. For more information on querying MongoDB, see our documentation on query documents.

You can also define additional query options such as sort and projection to configure the returned document. You can specify the additional options in the options object passed as the second parameter of the findOne method. For detailed reference documentation, see collection.findOne().

The following snippet finds a single document from the movies collection. It uses the following parameters:

  • A query document that configures the query to return only movies with the title of exactly the text 'The Room'.

  • A sort that organizes matched documents in descending order by rating, so if our query matches multiple documents the returned document will be the document with the highest rating.

  • A projection that explicitly excludes the _id field from returned documents and explicitly includes only the title and imdb object (and its embedded fields).

Note

You can use this example to connect to an instance of MongoDB and interact with a database that contains sample data. To learn more about connecting to your MongoDB instance and loading a sample dataset, see the Usage Examples guide.

If you run the preceding example, you should see the following output:

{ title: 'The Room', imdb: { rating: 3.5, votes: 25673, id: 368226 } }
←  Find OperationsFind Multiple Documents →