Docs Menu

Docs HomeDevelop ApplicationsMongoDB DriversNode.js

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 one of the following cursor methods:

  • next()

  • toArray()

  • forEach()

If no documents match the query, find() returns an empty cursor.

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 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: '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:

collection.find({ runtime: { $lt: 15 } }, { sort: { title: 1 }, projection: { _id: 0, title: 1, imdb: 1 }});
collection.find({ runtime: { $lt: 15 } }).sort({ title: 1}).project({ _id: 0, title: 1, imdb: 1 });
←  Find a DocumentInsert Operations →