Open In App

Mongoose Document Model.estimatedDocumentCount() API

Last Updated : 09 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Model.estimatedDocumentCount() method of the Mongoose API is used to count the number of documents in the MongoDB collection. It is useful for large collections. It is faster than other methods provided by mongoose because it collects metadata instead of scanning the entire collection.

Syntax:

Model.prototype.deleteOne() 

Parameters: The Model.prototype.deleteOne() method accepts four parameters:

  • options: It is an object with various properties.
  • callback: It is a callback function that will run once execution is completed.

Returns: The Model.estimatedDocumentCount() function returns a query object.

Setting up Node.js application:

Step-1: Create a Node.js application using the following command:

npm init

Step-2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this:

 

Example 1: In this example, We have established a database connection using mongoose and defined model over userSchema, having two columns or fields “name” and “age”. In the end, we are using estimatedDocumentCount() method on the User model which will return a query object.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require("mongoose");
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
// Defining userSchema
const userSchema = new mongoose.Schema({
    name: String,
    age: Number
});
  
// Defining userSchema model
const User = mongoose.model("User", userSchema);
const output = User.estimatedDocumentCount();
console.log(output)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

 

Example-2: In this example, We have established a database connection using mongoose and defined model over studentSchema, having three columns or fields “name”, “school”, and “class”. In the end, we are using estimatedDocumentCount() method on the Student model which will return a query object having meta data about the collection.

  • app.js: Write down the below code in the app.js file:

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    name: String,
    school: String,
    class: Number,
});
  
// Defining studentSchema model
const Student = mongoose.model("Student", studentSchema);
const output = Student.estimatedDocumentCount()
console.log(output)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.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