dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mongodb/#93
Script #93
mongodb
Replicationread-onlyMin Version: 4.0

MongoDB Oplog Size and Replication Window

Shows the oplog size and how many hours of operations it holds. Measures the risk of a secondary falling behind and needing a resync.

#oplog#replica-set#resync
mongodb · javascript
const o = db.getSiblingDB("local").oplog.rs;
const first = o.find().sort({ $natural: 1 }).limit(1).next().ts;
const last  = o.find().sort({ $natural: -1 }).limit(1).next().ts;
const stats = db.getSiblingDB("local").oplog.rs.stats();
({
  size_mb: Math.round(stats.maxSize / 1048576),
  used_mb: Math.round(stats.size / 1048576),
  window_hours: +((last.getTime() - first.getTime()) / 3600).toFixed(1)
});
Operational Notes & Remediation Guidance

Oplog window must exceed the duration required to restore and apply backups or survive secondary node outages.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 4.0).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.