Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js Driver

Find a Document

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 more query options such as sort and projection to configure the returned document. You can specify the more options in the options object passed as the second parameter of the findOne method. For detailed reference documentation, see collection.findOne().

You can use the Node.js driver to connect and use the findOne() method for deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB

  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB

To learn more about finding documents in the Atlas UI for deployments hosted in MongoDB Atlas, see Create, View, Update, and Delete Documents.

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.

Running the preceding example, you see the following output:

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