Docs Menu
Docs Home
/ / /
Node.js Driver
/ /

Count Documents

On this page

  • Overview
  • countDocuments() Example: Full File
  • API Documentation

In this guide, you can learn how to count the number of documents in your MongoDB collections. 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.

Tip

You can improve performance when using countDocuments() to return the total number of documents in a collection by avoiding a collection scan. To do this, use a hint to take advantage of the built-in index on the _id field. Use this technique only when calling countDocuments() with an empty query parameter.

collection.countDocuments({}, { hint: "_id_" });

Note

Example Setup

This example connects to an instance of MongoDB by using a connection URI. To learn more about connecting to your MongoDB instance, see the Connect to MongoDB guide. This example also uses the movies collection in the sample_mflix database included in the Atlas sample datasets. You can load them into your database on the free tier of MongoDB Atlas by following the Get Started with Atlas Guide.

Note

No TypeScript Specific Features

The following code example uses JavaScript. There are no TypeScript specific features of the driver relevant to this use case.

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:

1// Count documents in a collection
2
3import { MongoClient } from "mongodb";
4
5// Replace the uri string with your MongoDB deployment's connection string
6const uri = "<connection string uri>";
7
8const client = new MongoClient(uri);
9
10async function run() {
11 try {
12 const database = client.db("sample_mflix");
13 const movies = database.collection("movies");
14
15 /* Print the estimate of the number of documents in the
16 "movies" collection */
17 const estimate = await movies.estimatedDocumentCount();
18 console.log(`Estimated number of documents in the movies collection: ${estimate}`);
19
20 /* Print the number of documents in the "movies" collection that
21 match the specified query */
22 const query = { countries: "Canada" };
23 const countCanada = await movies.countDocuments(query);
24 console.log(`Number of movies from Canada: ${countCanada}`);
25 } finally {
26 // Close the connection after the operations complete
27 await client.close();
28 }
29}
30// Run the program and print any thrown exceptions
31run().catch(console.dir);

Running the preceding sample code results in the following output:

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

To learn more about any of the types or methods discussed in this guide, see the following API documentation:

Back

Specify a Query