skip to content

What do MongoDB's `mongostat` and `mongotop` each show, and where do their numbers come from?

level: juniorimportance: should knowfreq 27%

answer

  1. both run from the system shell
  2. whole server against per collection
  3. two different server commands
  4. rates, not lifetime totals

basics

~20 s

mongostat prints server-wide rates such as inserts, queries and connections per interval, built from serverStatus. mongotop prints milliseconds of read and write time per collection, built from the top command. Together they show how busy and where.

solid answer

~40 s

`mongostat` is the server-wide view. It polls `serverStatus` and prints a line per interval, one second by default: `insert`, `query`, `update`, `delete`, `getmore` and `command` rates, WiredTiger's `dirty` and `used` percentages, `flushes`, memory, queued and active clients, network and `conn`. A leading `*` marks replicated operations. It works against a `mongod` or a `mongos`, and `--discover` covers a whole deployment. `mongotop` is the per-collection view. It polls the `top` command and prints the milliseconds a `mongod` spent on each namespace, split into `total`, `read` and `write`. It rejects a `mongos`. Both run from the system shell, not from `mongosh`. So `mongostat` says the server got busier, and `mongotop` says which collection took the time.

code

bash · 5 lines
bash
# Three lines, each averaged per second over 5 seconds
mongostat --uri='mongodb://mongodb0.example.com:27017' --rowcount=3 5

# Time per collection on one mongod, every 30 seconds
mongotop 30 --uri='mongodb://mongodb0.example.com:27017'

go deeper

for a junior

Recall that mongostat gives server-wide rates and mongotop gives time per collection, that both run from the system shell, and that neither needs the profiler enabled.

for a middle

Explain that mongostat differences serverStatus samples and mongotop reads the top command, what the asterisk and read|write pairs mean, and why a total above the interval is normal.

for a senior

Use them for a quick read during an incident, then move to $currentOp and logs, knowing mongotop covers one mongod and that a sharded cluster needs a run per shard.

for a principal

Treat these as ad hoc tools for humans: ongoing measurement belongs in a monitoring system polling serverStatus, so that history survives after the terminal closes.

## Two tools, two questions `mongostat` and `mongotop` belong to the **MongoDB Database Tools**, which version separately from the server. They are command-line programs, so you run them from the system shell and not inside `mongosh`. Neither changes anything. They answer two different questions: - **`mongostat`:** how busy is this server overall? - **`mongotop`:** which collections is this `mongod` spending its time on? | | `mongostat` | `mongotop` | |---|---|---| | Granularity | One server, or all with `--discover` | One `mongod`, per namespace | | Data source | `serverStatus` | `top` | | Unit | Operations per second, sizes, counts | Milliseconds per interval | | Against a `mongos` | Works | Refused (tools 100.20.1) | | Default interval | 1 second | 1 second | | Privilege actions | `serverStatus` | `serverStatus` and `top` | ## Reading `mongostat` The header line looks like this: `insert query update delete getmore command dirty used flushes vsize res qrw arw net_in net_out conn time`. - **`insert`, `query`, `update`, `delete`, `getmore`:** operations per second. In the first four, a leading `*`, as in `*42`, means the figure is **replicated** operations applied on this member, which is what secondaries show because they apply writes rather than receive them from clients. A bare `*0` is simply how the tool prints zero when neither count moved, so it appears on any member. If a member has both kinds at once, the column reads `local|replicated`. - **`command`:** commands per second. On a secondary it appears as `local|replicated`. - **`dirty` and `used`:** the percentage of the WiredTiger cache holding dirty data and in use. These are gauges, and tuning them is a separate subject. - **`flushes`:** WiredTiger checkpoints triggered during the interval. - **`vsize` and `res`:** the process's virtual and resident memory. - **`qrw` and `arw`:** queued and active clients, shown as read|write. Non-zero queues that persist are worth following up, but treat these as a quick hint and confirm against `queues.execution` in `serverStatus`. - **`net_in`, `net_out`:** network traffic, which includes `mongostat`'s own. - **`conn`:** open connections. On a replica set, the output also has `set` and `repl`, where `repl` shows the member's state such as `PRI` or `SEC`. The interval is the last argument. With a value above 1, for example `mongostat 5`, each figure is the **average per second** over those 5 seconds, not a total. Options worth knowing include `--rowcount` (or `-n`) to stop after a number of lines, `--discover` to report on all members or shards, `--json` for scripts and `-o` to choose fields, including any `serverStatus` field. ## Reading `mongotop` Output has one row per namespace: - **`ns`:** `database.collection`. - **`total`:** milliseconds this `mongod` spent operating on that namespace. - **`read` and `write`:** the same time, split by kind. A few points prevent misreading: 1. Time is **across all clients**, so a value above the interval length is normal and means concurrent work. It is not a latency. 2. Only **active** namespaces appear. A missing collection simply had no recent activity. 3. It reports on **one `mongod`**. A sharded cluster means running it on each shard's members. Running it against a `mongos` fails with `cannot run mongotop against a mongos`, since `top` is not available there. 4. The `--locks` option is a leftover. It only works against MongoDB 2.6 or older instances and returns an error on newer ones. An interval is the positional argument here too: `mongotop 30` reports every 30 seconds. ## Where the numbers come from Both tools are thin clients that poll a server command and compute a difference between samples. That explains their behaviour: - `serverStatus` counters such as `opcounters` are cumulative since startup. `mongostat` subtracts two samples and divides by elapsed time, so you see rates. - You can get the same data yourself with `db.serverStatus()` when you need something `mongostat` does not print. - `top` keeps time per collection, and `mongotop` displays the change between polls. ## What they do not tell you These tools show how much work is happening and where, not why. Neither names a query or a client, and neither keeps history once the terminal closes. So the usual sequence is to see load rise in `mongostat`, spot the collection in `mongotop`, and then look at live operations with `$currentOp`, or at the slow-query log for what has already finished. Neither tool needs the database profiler to be on, because both read counters the server keeps anyway.

  • `mongotop` shows a `total` of 3,000 ms on a 1-second interval. Is something wrong?
    Not in itself. `total` adds up the time spent on that namespace across concurrent operations, so it can exceed the interval's wall-clock length. It tells you where a `mongod` is spending effort, not how long any single request took. Compare collections against each other, and use `$currentOp` or the slow-query log to find individual slow operations.
  • How would you watch a whole replica set with these tools?
    `mongostat --discover` connected to any member reports on all non-hidden members, one line per host per interval. `mongotop` has no such option and looks at one `mongod`, so you run it against each member you care about, usually the primary for writes and any secondary serving reads.

mongostat is a car's dashboard: speed, temperature and fuel for the whole vehicle. mongotop is a trip log showing which route took the driving time. Neither one tells you why a particular road was slow.

  • mongostat and mongotop are commands you type inside mongosh
  • mongotop reads its per-collection times from system.profile, so the profiler must be on
  • An interval of 5 in mongostat prints totals for those 5 seconds
  • mongotop's total is the latency a client saw on that collection
  • You can point mongotop at a mongos to get a view of a whole sharded cluster