Problem Statement
What is the oplog in MongoDB replication?
Explanation
The oplog, short for operations log, is a special capped collection that records all write operations on the primary node. Secondary nodes query the oplog and apply these operations to replicate data.
The oplog is stored in the local database and has a fixed size. When it reaches its size limit, old entries are overwritten. The oplog size should be large enough to hold operations for the time it takes a secondary to catch up if it falls behind. Oplog entries are idempotent, meaning they can be applied multiple times safely.
Code Solution
SolutionRead Only
// View oplog size
use local
db.oplog.rs.stats().maxSize
// View recent oplog entries
db.oplog.rs.find().sort({ $natural: -1 }).limit(5)
// Sample oplog entry
{
ts: Timestamp(1640000000, 1),
op: "i", // insert operation
ns: "mydb.users",
o: { _id: 1, name: "Alice" }
}