如何统计写入单个MongoDB实例的每日指标数量及命令建议
Hey there! Let me walk you through how to count the daily write metrics for a single MongoDB instance, plus share practical commands and methods to make this straightforward.
db.serverStatus() for Cumulative Write Counts MongoDB tracks core operation counters right in the server status. This is the quickest way to get aggregate write stats, though you'll need to calculate daily differences since these are cumulative values from instance startup.
Step-by-Step:
Connect to your MongoDB instance via the shell:
mongoRun the server status command to fetch write counters:
db.serverStatus().opcountersYou'll get output like this:
{ "insert" : 12345, "update" : 6789, "delete" : 123, "getmore" : 4567, "command" : 89012 }Focus on
insert,update, anddelete—these are your core write operations.To get daily counts:
- Capture the counter values at the start of the day (e.g., midnight)
- Capture them again at the end of the day
- Subtract the start values from the end values to get the daily write totals
mongostat for Real-Time & Scheduled Aggregated Stats mongostat is a command-line tool that outputs real-time instance metrics. You can configure it to run at intervals and capture a full day's worth of data, then aggregate the writes.
Example Command for Daily Stats:
This command collects data once per hour (3600 seconds) for 24 hours and saves it to a log file:
mongostat --rowcount 24 3600 >> /var/log/mongo_daily_writes_$(date +%Y-%m-%d).log
Analyzing the Output:
The log will have columns like insert, update, delete (each representing operations per second for that interval). To get daily totals:
- Multiply each interval's value by 3600 (seconds in an hour)
- Sum all those values for each write type
If you need more precise, per-operation write counts (and your instance has the oplog enabled—required for replica sets, optional for standalone instances), you can query the oplog directly.
Step-by-Step:
Enable oplog for standalone instances (if not already done):
Start MongoDB with the--replSetflag, initialize the replica set, and it'll create the oplog.Query the oplog for daily writes:
Switch to thelocaldatabase and run this query, replacing the date range with your target day:use local db.oplog.rs.find({ ts: { $gte: Timestamp(new ISODate("2024-05-20T00:00:00Z").getTime() / 1000, 0), $lte: Timestamp(new ISODate("2024-05-20T23:59:59Z").getTime() / 1000, 0) }, op: { $in: ["i", "u", "d"] } // "i"=insert, "u"=update, "d"=delete }).count()This returns the total number of write operations in the specified day. You can split it by operation type by adding a
$groupstage if needed.
For hands-off daily stats, create a simple script to capture and store write counts automatically. Here's a quick bash script example:
#!/bin/bash # Capture daily MongoDB write stats DATE=$(date +%Y-%m-%d) # Fetch cumulative counters at end of day WRITE_STATS=$(mongo --quiet --eval 'JSON.stringify(db.serverStatus().opcounters)') # Optional: Calculate delta if you saved start-of-day stats # START_STATS=$(cat /var/log/mongo_start_$DATE.json) # DELTA_INSERT=$(($(echo $WRITE_STATS | jq '.insert') - $(echo $START_STATS | jq '.insert'))) echo "$DATE,$WRITE_STATS" >> /var/log/mongo_daily_write_totals.log
Then set up a cron job to run this at midnight daily:
0 0 * * * /path/to/your/mongo_write_stats.sh
Key Notes:
- For replica sets, always collect stats from the primary node—that's where all writes happen before being replicated.
opcountersresets when the MongoDB instance restarts, so account for restarts in your daily calculations.- The oplog has a fixed size, so ensure it's large enough to retain at least a full day's worth of operations if using that method.
Hope these methods give you the daily write metrics you need! Let me know if you need help tweaking any of these commands for your specific setup.
内容的提问来源于stack exchange,提问作者l00p

