Mongoose Document Model.countDocuments() API
Last Updated :
30 Aug, 2022
The Model.countDocuments() method of the Mongoose API is used to count the number of document present in a collection. The countDocument() method count the number of documents in a collection based on matching filter.
Model.countDocuments() method accepts four parameters:
- filter: It is an object to filter out a document that needs to be updated.
- callback: It is a callback function which will run once execution is completed.
Return type: The Model.create() function returns a promise.
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:
Database Structure: The database structure will look like this, following documents are present in collection.
Example 1: In this example, We have established a database connection using mongoose and defined model over customerSchema, having two columns "name", and "orderCount". At the end, we are using countDocuments() method for getting count of documents in collection where value of "orderCount" is 10.
- 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(
"mongodb://localhost:27017/geeksforgeeks", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Defining customerSchema schema
const customerSchema = new mongoose.Schema({
name: String,
orderCount: Number,
});
// Defining customerSchema model
const Customer = mongoose.model("Customer", customerSchema);
// Calling countDocuments() on Customer model
Customer.countDocuments({ orderCount: 10 },
function (err, count) {
console.log("there are %d documents with orderCount
value as 10", count);
});
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
On Console:
there are 3 documents with orderCount value as 10
You can use any GUI tool for the representation of the database in graphical form. Here, I have used the Robo3T GUI tool for graphical representation.
Example 2: In this example, We have established a database connection using mongoose and defined model over customerSchema, having two columns "name", and "orderCount". At the end, we are using countDocuments() method for getting count of documents in collection where value of "name" is "Rahul".
- 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("mongodb://localhost:27017/geeksforgeeks", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Defining customerSchema schema
const customerSchema = new mongoose.Schema({
name: String,
orderCount: Number,
});
// Defining customerSchema model
const Customer = mongoose.model(
"Customer", customerSchema);
Customer.countDocuments({ name: "Rahul" },
function (err, count) {
console.log("there are %d documents
with name value as Rahul", count);
});
Steps to run the program: To run the application execute the below command from the root directory of the project:
node app.js
Output:
On Console:
there are 1 documents with name value as Rahul
You can use any GUI tool for the representation of the database in graphical form. Here, I have used the Robo3T GUI tool for graphical representation.
Reference: https://mongoosejs.com/docs/api/model.html#model_Model-countDocuments