Navigation

Count Documents

Info With Circle IconCreated with Sketch.Note

If you specify a callback method, countDocuments() and estimatedDocumentCount() return 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.

The Node.js driver provides two methods for counting documents in a collection:

  • collection.countDocuments() returns the number of documents in the collection that match the specified query. If you specify an empty query document, countDocuments() returns the total number of documents in the collection.
  • collection.estimatedDocumentCount() returns an estimation of the number of documents in the collection based on collection metadata.

estimatedDocumentCount() is faster than countDocuments() because the estimation uses the collection's metadata rather than scanning the collection. In contrast, countDocuments() takes longer to return, but provides an accurate count of the number of documents and supports specifying a filter. Choose the appropriate method for your workload.

To specify which documents you wish to count, countDocuments() accepts a query parameter. countDocuments() counts the documents that match the specified query.

countDocuments() and estimatedDocumentCount() support optional settings that affect the method's execution. Refer to the reference documentation for each method for more information.

The following example estimates the number of documents in the movies collection in the sample_mflix database, and then returns an accurate count of the number of documents in the movies collection with Canada in the countries field.

Info With Circle IconCreated with Sketch.Note

This example connects to an instance of MongoDB using a connection string. To learn more about connecting to your MongoDB instance, see the connection 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);
async function run() {
try {
await client.connect();
const database = client.db("sample_mflix");
const movies = database.collection("movies");
// Estimate the total number of documents in the collection
// and print out the count.
const estimate = await movies.estimatedDocumentCount();
console.log(`Estimated number of documents in the movies collection: ${estimate}`);
// Query for movies from Canada.
const query = { countries: "Canada" };
// Find the number of documents that match the specified
// query, (i.e. with "Canada" as a value in the "countries" field)
// and print out the count.
const countCanada = await movies.countDocuments(query);
console.log(`Number of movies from Canada: ${countCanada}`);
} finally {
await client.close();
}
}
run().catch(console.dir);

If you run the sample code above, you should see output that resembles the following:

Estimated number of documents in the movies collection: 23541
Number of movies from Canada: 1349
Give Feedback