MongoDB Monitoring
A running database needs regular health checks, just like a car needs attention to its dashboard lights. Monitoring means watching key numbers, such as connections, speed, memory, and storage, so you can spot trouble before users notice. Early warning turns a late-night emergency into a calm fix during work hours.
The Car Dashboard Picture
A dashboard shows speed, fuel, and engine temperature at a glance. A MongoDB monitoring setup shows the same kind of summary for the database.
+------------------------------------------------------------+
| MongoDB Dashboard |
| |
| Connections: 120 / 800 Operations/sec: 2,400 |
| Memory used: 71 % Disk free: 38 % |
| Replication lag: 0 s Slow queries (last hour): 3 |
+------------------------------------------------------------+
green = healthy yellow = watch red = act nowKey Metrics to Watch
| Metric | What It Tells You | Warning Sign |
|---|---|---|
| Operations per second | How busy the database is | Sudden spikes or sudden drops |
| Connections | How many clients are connected | Count close to the maximum limit |
| Query execution time | How fast requests finish | Time growing over days |
| Memory and cache use | Whether data fits in RAM | Constant cache eviction |
| Disk space | Room left for data and logs | Less than 20 percent free |
| Replication lag | Delay between primary and secondary servers | Lag growing past a few seconds |
| Errors and assertions | Failed operations | Rising counts |
Built-in Shell Commands
serverStatus: The Full Report
db.serverStatus()This command returns a large report. Useful sections include connections, opcounters, mem, and network. Print a single section for a quick look:
db.serverStatus().connections
db.serverStatus().opcountersThe connections section shows the current and available connection counts. The opcounters section counts inserts, queries, updates, and deletes since the server started.
Database and Collection Size
db.stats()
db.orders.stats()These commands report data size, storage size, and index size.
Running Operations
db.currentOp({ secs_running: { $gte: 3 } })The command lists operations that have run for three seconds or longer. Stop a stuck operation with db.killOp(opId) after checking that it is safe.
Replica Set Health
rs.status()
rs.printSecondaryReplicationInfo()The first command shows the state of each member. The second command shows how far each secondary server trails behind the primary.
Command-Line Monitoring Tools
MongoDB ships small tools that print live numbers in a terminal.
mongostat
mongostat --uri="mongodb://localhost:27017" 5The tool prints one line every 5 seconds with inserts, queries, updates, deletes, connections, and memory use. It acts like a live heartbeat monitor.
mongotop
mongotop 10The tool shows which collections spend the most time on reads and writes during each 10 second window. Use it to find the busiest collection.
Slow Query Detection
The profiler records operations that exceed a time limit:
db.setProfilingLevel(1, { slowms: 100 })
db.system.profile.find().sort({ ts: -1 }).limit(5)MongoDB writes slow operations into its log file as well. Search the log for lines that mention slow queries to find repeated trouble spots.
Cloud and Dashboard Tools
- MongoDB Atlas includes built-in charts, a performance advisor, and alert rules for hosted clusters.
- MongoDB Cloud Manager and Ops Manager provide monitoring and backup management for self-managed servers.
- Third-party tools such as Prometheus with Grafana collect MongoDB metrics and draw custom dashboards.
Alerts That Matter
Alerts watch the numbers for you. Start with a short list of rules that point to real problems:
- Disk space drops below 20 percent.
- Connections pass 80 percent of the limit.
- Replication lag stays above 10 seconds for several minutes.
- A replica set member goes down or loses its primary role.
- Slow queries rise sharply compared with a normal day.
Send alerts to a channel that people check, such as email or a team chat room. Too many alerts teach people to ignore them, so review the rules every few months and remove noise.
Build a Habit
- Record normal numbers during a calm week. A baseline makes strange numbers easy to notice.
- Check the dashboard after every release of the application.
- Review slow queries every week and add indexes where they help.
- Practice a failover test on a replica set so the team knows what the alerts look like.
Summary
Monitoring keeps a MongoDB deployment healthy by tracking operations, connections, memory, disk space, and replication lag. Shell commands such as serverStatus, currentOp, and rs.status give instant reports, while mongostat and mongotop show live activity. The profiler finds slow queries, and platforms such as Atlas add charts and alerts. A clear baseline and a short list of useful alerts catch problems early.
