What is the oplog in MongoDB?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for MongoDB development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

The oplog (operations log) is a special capped collection in the local database on each MongoDB replica set member that records all data modification operations in the order they occurred. It is the mechanism enabling replication — secondaries continuously tail the primary's oplog and replay the operations to stay synchronized. Oplog characteristics: (1) Capped collection: fixed size, automatically overwrites oldest entries when full. Default size: 5% of free disk space (at least 1GB). Configure with oplogSizeMB; (2) Idempotent operations: operations in the oplog are idempotent — can be applied multiple times with the same result. MongoDB transforms operations if needed (e.g., $inc: 1 becomes $set: newValue); (3) Oplog window: how far back in time the oplog goes. Determines recovery window — if a secondary falls this far behind, it can no longer replicate and needs a full resync; (4) Oplog entry structure: { "ts": Timestamp, "op": "i/u/d/c", "ns": "database.collection", "o": { document }, "o2": { query } }. ops: i (insert), u (update), d (delete), c (command), n (noop). Change Data Capture (CDC): change streams (MongoDB 3.6+) provide a higher-level, resumable, filtered view of the oplog — preferred over directly reading the oplog in applications. Monitoring oplog lag: rs.printReplicationInfo() shows oplog window and secondary lag.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a MongoDB codebase.