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.
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 });