Find Multiple Documents
You can query for multiple documents in a collection with
collection.find()
. The find()
method uses a query document that you
provide to match 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 returns all documents in the collection. 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 result set. You can specify these in the options
parameter in your find()
method call in sort
and projection
objects. See collection.find() for more
information on the parameters you can pass to the method.
The find()
method returns a FindCursor that
manages the results of your query. You can iterate through the matching
documents using the for await...of
syntax, or one of the following
cursor methods:
next()
toArray()
If no documents match the query, find()
returns an empty cursor.
Compatibility
You can use the Node.js driver to connect and use the find()
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.
Example
The following snippet finds documents from the movies
collection. It
uses the following parameters:
A query document that configures the query to return only movies with a runtime of less than 15 minutes.
A sort that organizes returned documents in ascending order by title (alphabetical order in which "A" comes before "Z" and "1" before "9").
A projection that explicitly excludes the
_id
field from returned documents and explicitly includes only thetitle
andimdb
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.
1 import { MongoClient } from "mongodb"; 2 3 // Replace the uri string with your MongoDB deployment's connection string. 4 const uri = "<connection string uri>"; 5 6 const client = new MongoClient(uri); 7 8 async function run() { 9 try { 10 11 // Get the database and collection on which to run the operation 12 const database = client.db("sample_mflix"); 13 const movies = database.collection("movies"); 14 15 // Query for movies that have a runtime less than 15 minutes 16 const query = { runtime: { $lt: 15 } }; 17 18 const options = { 19 // Sort returned documents in ascending order by title (A->Z) 20 sort: { title: 1 }, 21 // Include only the `title` and `imdb` fields in each returned document 22 projection: { _id: 0, title: 1, imdb: 1 }, 23 }; 24 25 // Execute query 26 const cursor = movies.find(query, options); 27 28 // Print a message if no documents were found 29 if ((await movies.countDocuments(query)) === 0) { 30 console.log("No documents found!"); 31 } 32 33 // Print returned documents 34 for await (const doc of cursor) { 35 console.dir(doc); 36 } 37 38 } finally { 39 await client.close(); 40 } 41 } 42 run().catch(console.dir);
1 import { MongoClient } from "mongodb"; 2 3 // Replace the uri string with your MongoDB deployment's connection string. 4 const uri = "<connection string uri>"; 5 6 const client = new MongoClient(uri); 7 8 type Minutes = number; 9 10 interface IMDB { 11 rating: number; 12 votes: number; 13 id: number; 14 } 15 16 interface Movie { 17 title: string; 18 imdb: IMDB; 19 runtime: Minutes; 20 } 21 22 async function run() { 23 try { 24 const database = client.db("sample_mflix"); 25 const movies = database.collection<Movie>("movies"); 26 27 const query = { runtime: { $lt: 15 } }; 28 const cursor = movies.find<Movie>( 29 query, 30 { 31 sort: { title: 1 }, 32 projection: { _id: 0, title: 1, imdb: 1 }, 33 } 34 ); 35 36 if ((await movies.countDocuments(query)) === 0) { 37 console.warn("No documents found!"); 38 } 39 40 for await (const doc of cursor) { 41 console.dir(doc); 42 } 43 } finally { 44 await client.close(); 45 } 46 } 47 run().catch(console.dir);
If you run the preceding example, you should see the following output:
{ title: '10 Minutes', imdb: { rating: 7.9, votes: 743, id: 339976 } } { title: '3x3', imdb: { rating: 6.9, votes: 206, id: 1654725 } } { title: '7:35 in the Morning', imdb: { rating: 7.3, votes: 1555, id: 406501 } } { title: '8', imdb: { rating: 7.8, votes: 883, id: 1592502 } } ...
The sort
and projection
options can also be specified as methods
(sort()
and project()
, respectively) chained to the find()
method.
The following two commands are equivalent:
movies.find({ runtime: { $lt: 15 } }, { sort: { title: 1 }, projection: { _id: 0, title: 1, imdb: 1 }}); movies.find({ runtime: { $lt: 15 } }).sort({ title: 1}).project({ _id: 0, title: 1, imdb: 1 });