You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何统计写入单个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.

1. Use MongoDB's Built-in 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:

  1. Connect to your MongoDB instance via the shell:

    mongo
    
  2. Run the server status command to fetch write counters:

    db.serverStatus().opcounters
    

    You'll get output like this:

    {
      "insert" : 12345,
      "update" : 6789,
      "delete" : 123,
      "getmore" : 4567,
      "command" : 89012
    }
    

    Focus on insert, update, and delete—these are your core write operations.

  3. 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
2. Use 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
3. Dig into the Oplog for Granular Write Details

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:

  1. Enable oplog for standalone instances (if not already done):
    Start MongoDB with the --replSet flag, initialize the replica set, and it'll create the oplog.

  2. Query the oplog for daily writes:
    Switch to the local database 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 $group stage if needed.

4. Automate with a Custom Script

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.
  • opcounters resets 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:09:47