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 Cursor 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

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 movies that have a runtime less than 15 minutes
const query = { runtime: { $lt: 15 } };
const options = {
// sort returned documents in ascending order by title (A->Z)
sort: { title: 1 },
// Include only the `title` and `imdb` fields in each returned document
projection: { _id: 0, title: 1, imdb: 1 },
};
const cursor = movies.find(query, options);
// print a message if no documents were found
if ((await cursor.count()) === 0) {
console.log("No documents found!");
}
// replace console.dir with your callback to access individual elements
await cursor.forEach(console.dir);
} finally {
await client.close();
}
}
run().catch(console.dir);

If you run the example above, you should see results that resemble the following:

{ 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 →