MongoDB provides several built-in diagnostic tools to monitor and analyze database performance.Two of the most commonly used tools are mongotop and mongostat. These utilities offer real-time insights into database activity, helping administrators troubleshoot performance issues, identify slow operations, and optimize resource usage.

Key features

Mongostat

  • Real-time Summary:
    Provides a live, high-level overview of database activity including inserts, queries, updates, deletes, and commands.
  • Operation Rate Monitoring:
    Tracks the rate of key MongoDB operations per second, helping you understand your system’s workload.
  • Connection and Memory Metrics:
    Displays the number of active connections and memory usage statistics, which are vital for performance tuning.
  • Performance Issue Detection:
      Helps quickly spot spikes, drops, or unusual patterns in database activity, making it easier to diagnose problems.
  • Lightweight and Easy to Use:
    Runs from the command line without putting significant load on the MongoDB server.

Mongotop

  • Per-Collection Insights:
    Displays detailed read and write activity for each collection, helping you understand which parts of your database are most active.
  • Real-time Monitoring:
    Continuously tracks database operations in real-time, providing a live view of workload distribution.
  • Performance Bottleneck Identification:
    Helps quickly identify collections causing heavy load or delays, making it easier to troubleshoot and optimize performance.
  • Simple and Lightweight:
    Easy to run from the command line without introducing additional overhead on your MongoDB server.

Syntax & Options

mongostat

Syntax

mongostat <options> < connection-string> <polling interval in seconds>

Options

  • –o
    It specifies fields to include in the output
    Supports two methods:
    . diff() : How much a serverStatus  field has changed since the previous mongostat call.
    rate() : The rate  per  second at which a serverStatus field changes from mongostat call to call.

    Note :
    1) The diff() and rate() are alternative modes of viewing the data in mongostat. The diff() function shows the difference in counts between each polling interval, while rate() displays the rate per second for each field. Since both functions modify the output style in different ways, MongoDB tools do not allow combining them in a single command.
    2) The diff() and rate() are internal modes in the MongoDB Go driver and in some monitoring tools, but NOT available as direct command-line options for the mongostat CLI tool.
  • –O
    It returns serverStatus  fields and standard mongostat output.
  • –rowcount
    Limits the no of rows displayed.

mongotop

Syntax

Mongotop <options> <connection-string> <polling interval in seconds>

Example

mongotop 127.0.0.1:27017 5

In this example, mongotop connects to MongoDB running on 127.0.0.1:27017. Every 5 seconds, it prints a table showing how much time MongoDB spends reading and writing for each collection.

Options

  • –rowcount :  Limits the number of rows displayed.
  • –json : It returns output in JSON format and also returns   count of the no  of operations which took  place during the polling interval.

Examples

Mongostat

  1. Running mongostat on a Specific Database
    You can run mongostat and connect it to a specific database (for example, mydb) using the connection string:
mongostat "mongodb://127.0.0.1:27017/mydb"

This command connects to the mydb database on the local MongoDB server and displays real-time statistics.

mongostat outputs the following fields:

FieldDescription
insertThe number of objects inserted into the database per second
queryThe number of query operations per second.
updateThe number of update operations per second
deleteThe number of delete operations per second
getmoreThe number of get more (i.e. cursor batch) operations per second
commandThe number of commands per second On secondary systems
flushesNumber of times per second the database flushes data to disk.
dirtyPercentage of data that is dirty (modified but not yet written to disk).
usedPercentage of space in the cache that is actively used
vsizeVirtual memory size used by the MongoDB process.
resResident memory size (physical RAM) used by MongoDB
lockedThe percent of time in a global write lock
qrThe length of the queue of clients waiting to read data from the MongoDB instance
qwThe length of the queue of clients waiting to write data from the MongoDB instance
arActive readers -The number of clients currently reading.
awActive writers – The number of clients currently writing
net_inAmount of incoming network traffic (bytes per second).
net_outAmount of outgoing network traffic (bytes per second).
connNumber of open connections to the database.
setNumber of open connections to the database.
replThe replication status of the member.

2) To display only the host, insert rate, query rate, and command rate fields from mongostat, and limit the output to 3 lines, you can use:

mongostat -o='host,opcounters.insert.rate()=Insert Rate,opcounters.query.rate()=Query Rate,opcounters.command.rate()=Command Rate' --rowcount=3  "mongodb://127.0.0.1:27017/mydb"

This provides a focused view on operation rates, which is useful for quick performance checks.

Mongotop

To monitor MongoDB read and write activity every 2 seconds and limit the output to 4 lines:

mongotop 2 "mongodb://127.0.0.1:27017" --rowcount=4

FieldsDescription
nsContains the database namespace, which combines the database name and collection
dbContains the name of the database
totalProvides the total amount of time that this mongod spent operating on this namespace
readProvides the amount of time that this mongod spent performing read operations on this namespace.
writeProvides the amount of time that this mongod spent performing write operations on this namespace.

Conclusion

MongoDB’s built-in diagnostic tools, mongotop and mongostat, provide essential insights into database performance and resource utilization. While mongotop helps pinpoint collection-level activity, mongostat gives a broader view of database operations. By incorporating these tools into your monitoring strategy, you can proactively manage performance issues, optimize queries, and ensure a smooth database experience.

Regularly using these tools will help keep your MongoDB deployments efficient and responsive. For deeper analysis, consider integrating them with more advanced monitoring solutions like Prometheus or Percona Monitoring and Management (PMM).

Stay tuned for more in-depth MongoDB performance tuning tips!


Discover more from Genexdbs

Subscribe now to keep reading and get access to the full archive.

Continue reading