Open In App

Mongoose Query.prototype.estimatedDocumentCount() API

Last Updated : 23 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The estimatedDocumentCount() method is part of the Mongoose Query API and is used to estimate the number of documents in a collection. It provides an approximate count of the documents using metadata, which is faster than counting all documents in the collection.

Syntax:

const MyModel = mongoose.model('MyModel', mySchema);
MyModel.estimatedDocumentCount()
    .then((count) => {
        ...
    })
    .catch((err) => {
        console.error(err);
    });

Installation of mongoose module:

Step 1: You can install this package by using this command.

npm install mongoose

Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

Step 3: After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.

node index.js

Project Structure: The project structure will look like this:

 

Example 1: In this example, estimatedDocumentCount() is used to get the approximate number of product documents in the shopDB collection. The Product model is defined with a schema that has four fields: id, name, price and stock. The estimatedDocumentCount() method is called on the Product model to get the approximate count of documents, which is logged to the console.

  • Index.js

Javascript




const mongoose = require("mongoose");
  
mongoose.set("strictQuery", true);
mongoose
    .then(() => {
        const products = mongoose.model("Products", {
            id: Number,
            name: String,
            price: Number,
            stock: Number,
        });
  
        products
            .estimatedDocumentCount()
            .then((count) => {
                console.log(
`There are approximately ${count} products in the collection.`
                );
                mongoose.connection.close();
            })
            .catch((err) => console.error(err));
    })
    .catch((err) => console.error(err));
//Code contributed by Anurag Sharma


Steps to run the application: Run the index.js file using the below command:

Step 1: Make sure you have installed the mongoose module using the following command:

npm install mongoose

Step 2: Below is the sample data in the database before the function is executed, You can use any GUI tool or terminal to see the database like we have used the MongoDB compass GUI tool as shown below:

 

Step 3: Run the index.js file using the below command:

node index.js

Output:

 

Example 2: In this example, estimatedDocumentCount() is used to get the approximate number of User documents in the cseSociety collection.

Javascript




const mongoose = require("mongoose");
  
mongoose.set("strictQuery", true);
mongoose
    .then(() => {
        const users = mongoose.model("Users", {
            fullName: String,
            userName: String,
            email: String,
            phone: Number,
        });
  
        users
            .estimatedDocumentCount()
            .then((count) => {
                console.log(
`There are approximately ${count} users in the collection.`
                );
                mongoose.connection.close();
            })
            .catch((err) => console.error(err));
    })
    .catch((err) => console.error(err));
//Code contributed by Anurag Sharma


Steps to run the application: Run the index.js file using the below command:

Step 1: In this example, we are using the below document:

 

Step 3: Run the index.js file using the below command:

node index.js

Output:

 

Reference: https://mongoosejs.com/docs/api/model.html#model_Model.estimatedDocumentCount



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads