Master MongoDB Monitoring: Using mongostat and db.stats() for Performance Insights
This guide explains how to use MongoDB's built-in mongostat tool and key database functions like db.stats() and db.serverStatus(), detailing command options, output metrics, and how to interpret them for diagnosing performance issues and monitoring server health.
1. mongostat Tool Overview
mongostat is a MongoDB‑provided command‑line utility that periodically reports the current state of a MongoDB instance. It is useful for quickly spotting slowdowns or other issues.
mongostat Command Options
--help : display help information
--version : show MongoDB version
--host : specify the host (and optionally the port)
--port : specify the port if not included in --host --sslCAFile : path to the CA certificate
--username : username for authentication
--password : password for authentication
Typical usage example:
mongostat --rowcount 2 --host 192.168.0.8:27027Output Metrics Explanation
inserts/s : inserts per second
query/s : queries per second
update/s : updates per second
delete/s : deletes per second
getmore/s : getmore operations per second
command/s : total commands per second (includes all above)
flushes/s : fsync operations per second
mapped/s : amount of data memory‑mapped (MB)
vsize : virtual memory size (MB)
res : resident (physical) memory size (MB)
faults/s : page faults per second (Linux only)
locked % : percentage of time the database is locked (aim < 50%)
idx miss % : index miss percentage
qt|r|w : queue lengths for total, read, and write operations
ar : active client write operations
conn : current connections
time : timestamp
2. MongoDB Built‑in Functions
db.stats() Function
Provides database‑level statistics such as collection count, object count, average object size, data size, storage size, index information, and more.
use test
db.stats()
{
"collections" : 9,
"objects" : 4278845,
"avgObjSize" : 224.56603031892953,
"dataSize" : 960883236,
"storageSize" : 1195438080,
"numExtents" : 59,
"indexes" : 13,
"indexSize" : 801931264,
"fileSize" : 6373244928,
"ok" : 1
}Key output fields:
collections : number of collections
objects : estimated total document count
avgObjSize : average document size (bytes)
dataSize : total size of all document data (bytes)
storageSize : allocated storage size on disk (bytes)
indexes : number of indexes
indexSize : total size of indexes (bytes)
fileSize : pre‑allocated file size for the database
db.serverStatus() Function
Returns a comprehensive snapshot of the server’s status, including version, uptime, memory usage, connection counts, lock statistics, index counters, background flushing metrics, cursor information, replication role, operation counters, and assertion counts.
{
"version" : "1.6.5",
"uptime" : 7208469,
"mem" : {
"bits" : 64,
"resident" : 3131,
"virtual" : 6172,
"mapped" : 4927
},
"connections" : {
"current" : 402,
"available" : 2599
},
"globalLock" : {
"totalTime" : 7208469556704,
"lockTime" : 4959693717,
"ratio" : 0.000688036992871448,
"currentQueue" : { "total" : 0, "readers" : 0, "writers" : 0 }
},
"indexCounters" : { "btree" : { "accesses" : 2821726, "hits" : 2821725, "misses" : 1, "missRatio" : 3.543930204420982e-7 } },
"backgroundFlushing" : { "flushes" : 120133, "total_ms" : 73235923, "average_ms" : 609.6236920746173, "last_ms" : 1332 },
"opcounters" : { "insert" : 269351, "query" : 19331151, "update" : 14199331, "delete" : 1, "getmore" : 145575, "command" : 55982302 },
"asserts" : { "regular" : 0, "warning" : 0, "msg" : 0, "user" : 27, "rollovers" : 0 },
"ok" : 1
}Important fields to monitor:
Host, version, process : basic instance identification
Uptime / uptimeEstimate : how long the server has been running
globalLock.* : lock time and ratio, queue lengths
mem.resident / mem.virtual / mem.mapped : memory usage details
connections.current / connections.available : active and available connections
indexCounters.btree.* : index access statistics
backgroundFlushing.* : disk flush frequency and latency
opcounters.* : cumulative operation counts since startup
asserts.* : assertion occurrences
By regularly reviewing these metrics, administrators can quickly identify performance bottlenecks, memory pressure, lock contention, and other issues affecting MongoDB stability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
