Open In App

Understanding Mongoose Connection.readyState

Last Updated : 16 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Connection.prototype.readyState property in Mongoose allows developers to track and manage the current state of a database connection in a MongoDB application. This property returns a numeric value that corresponds to the connection’s current state, providing a simple way to monitor whether the connection is in a disconnected, connecting, connected, or disconnecting state. Understanding how to properly use readyState is crucial for building robust, efficient, and responsive Node.js applications using Mongoose.

What is Mongoose Connection.readyState?

The readyState property in Mongoose helps determine the status of your MongoDB connection. The returned numeric value represents the current state of the connection object. This feature is particularly useful for debugging and ensuring that your application behaves correctly as it establishes, maintains, or disconnects from the MongoDB database.

The following list represent different connection ready states:

  • 0: disconnected
  • 1: connecting 
  • 2: connected 
  • 3: disconnecting

Syntax:

connection.readyState;

Parameters

This property does not take any parameters. It is simply accessed via the connection object.

Return Value

This method returns numeric number based on event name which represents the current state of the connection object.

  • 0: disconnected
  • 1: connecting
  • 2: connected
  • 3: disconnecting

Setting up Node.js with Mongoose

Before diving into using the readyState property, ensure that your Node.js environment is set up correctly with Mongoose.

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: Tracking Connection State with readyState

The below example illustrates the basic functionality of the Mongoose Connection readyState property. We are able to track connection object current state using this property.

Filename: app.js

JavaScript
// Require mongoose module
const mongoose = require("mongoose");

// Set Up the Database connection
const URI = "mongodb://localhost:27017/geeksforgeeks"

const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

connectionObject.on('connected', () => {
    console.log('Connected', connectionObject.readyState);
});

Step to run the program:

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

node app.js

Output:

Connected to MongoDB!
Connection State: 1

Explanation: This example demonstrates how the readyState property helps confirm that the connection to MongoDB has been established (state 1: connected).

Example 2: Transitioning Through Connection States

The below example illustrates the basic functionality of the Mongoose Connection readyState property. We are able to track connection object current state using this property. At the end, we are calling close() method in connected event in order to close the connection, and we can see connection object in disconnecting and disconnected state.

Filename: app.js

JavaScript
// Require mongoose module
const mongoose = require("mongoose");

// Set Up the Database connection
const URI = "mongodb://localhost:27017/geeksforgeeks"

const connectionObject = mongoose.createConnection(URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

connectionObject.on('connected', () => {
    console.log('Connected', connectionObject.readyState);
    connectionObject.close()
});

connectionObject.on('disconnecting', () => {
    console.log('Disconnecting ', connectionObject.readyState);
});

connectionObject.on('disconnected', () => {
    console.log('Disconnected ', connectionObject.readyState);
});

Step to run the program:

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

node app.js

Output:

Connected to MongoDB!
Connection State: 1
Disconnected from MongoDB!
Connection State: 0

Explanation: In this example, we observe how the readyState property reflects the connection state before and after the close() method is called.

Conclusion

The Connection.prototype.readyState property in Mongoose is a valuable tool for monitoring the state of your MongoDB connection. It helps you understand and track the transition between different connection states (disconnected, connecting, connected, and disconnecting). By utilizing this property, you can make more informed decisions when handling database connections, ensuring that your application behaves reliably during connection and disconnection events.


Next Article

Similar Reads