Docs Home → Develop Applications → MongoDB Drivers → Node.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().
Example
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 thetitle
andimdb
object (and its embedded fields).
Note
This example connects to an instance of MongoDB and uses a sample data database. To learn more about connecting to your MongoDB instance and loading this database, see the Usage Examples guide.
const { MongoClient } = require("mongodb"); // Replace the uri string with your MongoDB deployment's connection string. const uri = "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority"; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, }); async function run() { try { await client.connect(); const database = client.db("sample_mflix"); const movies = database.collection("movies"); // Query for a movie that has the title 'The Room' const query = { title: "The Room" }; const options = { // sort matched documents in descending order by rating sort: { "imdb.rating": -1 }, // Include only the `title` and `imdb` fields in the returned document projection: { _id: 0, title: 1, imdb: 1 }, }; const movie = await movies.findOne(query, options); // since this method returns the matched document, not a cursor, print it directly console.log(movie); } finally { await client.close(); } } run().catch(console.dir);
If you run the example above, you should see a result that resembles the following:
{ title: 'The Room', imdb: { rating: 3.5, votes: 25673, id: 368226 } }