Get the number of open connections in MongoDB?

To get the number of open connections in MongoDB, use the serverStatus() method which returns comprehensive server statistics including connection information.

Syntax

db.serverStatus()

Example

Execute the following command to retrieve server status ?

db.serverStatus()

This will produce output with various server metrics. Look for the connections section ?

{
    "host" : "DESKTOP-QN2RB3H",
    "version" : "4.0.5",
    "process" : "mongod",
    "connections" : {
        "current" : 1,
        "available" : 999999,
        "totalCreated" : 1
    },
    // ... other server statistics
}

Key Points

  • current: Number of currently active/open connections
  • available: Number of additional connections the server can accept
  • totalCreated: Total number of connections created since server start

Get Only Connection Information

To retrieve only connection-related statistics, use ?

db.serverStatus().connections
{
    "current" : 1,
    "available" : 999999,
    "totalCreated" : 1
}

Conclusion

Use db.serverStatus().connections.current to get the number of open connections. The serverStatus() method provides comprehensive server metrics including active connections, available connection slots, and total connections created.

Updated on: 2026-03-15T02:27:48+05:30

471 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements