0
0
MongodbHow-ToBeginner · 3 min read

How to Get Server Status in MongoDB: Syntax and Examples

To get the server status in MongoDB, use the db.serverStatus() command in the MongoDB shell. This command returns detailed information about the database server's health and performance.
📐

Syntax

The db.serverStatus() command is run in the MongoDB shell to retrieve the current status of the server. It returns a document with various statistics and metrics about the server.

  • db: The current database connection object.
  • serverStatus(): The method that fetches server status information.
mongodb
db.serverStatus()
💻

Example

This example shows how to run db.serverStatus() in the MongoDB shell to get server details like uptime, connections, and memory usage.

mongodb
/* Connect to MongoDB shell and run: */
db.serverStatus()
Output
{ "host" : "myServer", "version" : "6.0.3", "process" : "mongod", "uptime" : 123456, "connections" : { "current" : 10, "available" : 1000 }, "mem" : { "resident" : 512, "virtual" : 1024 }, "ok" : 1 }
⚠️

Common Pitfalls

Some common mistakes when getting server status in MongoDB include:

  • Running serverStatus() on a non-admin database may return limited information.
  • Expecting the command to work without proper permissions; you need appropriate user roles.
  • Misinterpreting the output fields; some metrics require understanding MongoDB internals.
mongodb
/* Wrong: Running on a non-admin database might limit info */
db = db.getSiblingDB('test')
db.serverStatus()  // May not show full server info

/* Right: Use the admin database */
db = db.getSiblingDB('admin')
db.serverStatus()  // Full server status info
📊

Quick Reference

CommandDescription
db.serverStatus()Returns server status document with metrics
db.getSiblingDB('admin')Switch to admin database for full status
db.serverStatus().uptimeShows server uptime in seconds
db.serverStatus().connectionsShows current and available connections
db.serverStatus().memShows memory usage statistics

Key Takeaways

Use db.serverStatus() in the MongoDB shell to get detailed server health info.
Run the command on the admin database for full access to server metrics.
Ensure your user has the right permissions to execute serverStatus().
The output includes uptime, connections, memory, and other important server stats.
Understanding the output helps monitor and troubleshoot MongoDB server performance.