The oplog helps keep copies of data in sync across multiple MongoDB servers. It records changes so all copies stay updated.
Oplog and replication mechanism in MongoDB
Introduction
Syntax
MongoDB
No direct query syntax; oplog is a special collection named 'oplog.rs' in the local database used internally by MongoDB replication.
The oplog is a capped collection that stores a rolling log of all operations that modify the data.
Replication uses the oplog to copy changes from the primary server to secondary servers.
Examples
MongoDB
use local db.getCollection('oplog.rs').find().limit(5).sort({$natural:-1})
MongoDB
rs.status()
MongoDB
rs.initiate()
Sample Program
This sequence starts a replica set, checks its status, and shows the last 3 operations in the oplog.
MongoDB
rs.initiate() rs.status() use local db.getCollection('oplog.rs').find().limit(3).sort({$natural:-1})
Important Notes
The oplog is only accessible on replica set members, not standalone servers.
Oplog entries include the operation type: insert (i), update (u), delete (d), etc.
Replication ensures data is copied from the primary to secondaries by replaying oplog entries.
Summary
The oplog is a special log that records all data changes in MongoDB replica sets.
Replication uses the oplog to keep multiple servers' data in sync automatically.
This helps with data safety, scaling, and availability.
Practice
1. What is the primary purpose of the
oplog in MongoDB replica sets?easy
Solution
Step 1: Understand the role of oplog in MongoDB
The oplog is a special log that records every data change in a replica set.Step 2: Connect oplog to replication
Replication uses the oplog to copy changes from the primary to secondary servers.Final Answer:
To record all data changes for replication -> Option CQuick Check:
Oplog records changes [OK]
Hint: Oplog logs changes for syncing replicas [OK]
Common Mistakes:
- Confusing oplog with user data storage
- Thinking oplog manages backups
- Assuming oplog stores indexes
2. Which of the following is the correct command to view the oplog entries in a MongoDB replica set?
easy
Solution
Step 1: Recall the oplog collection name
The oplog is stored in the local database under the collection namedoplog.rs.Step 2: Identify the correct query syntax
To view entries, usedb.oplog.rs.find()in the mongo shell.Final Answer:
db.oplog.rs.find() -> Option AQuick Check:
Correct oplog collection query [OK]
Hint: Oplog collection is oplog.rs in local DB [OK]
Common Mistakes:
- Using wrong collection name like oplog or oplogrs
- Trying to call oplog as a function
- Missing the dot between oplog and rs
3. Given a MongoDB replica set, what will happen if the oplog on the primary server is deleted accidentally?
medium
Solution
Step 1: Understand oplog role in replication
The oplog stores all changes that secondaries use to replicate data.Step 2: Consequence of oplog deletion on primary
If oplog is deleted, secondaries lose the source of changes and stop replicating, causing stale data.Final Answer:
Secondaries will stop replicating and may become stale -> Option AQuick Check:
Oplog missing = replication stops [OK]
Hint: No oplog means no replication updates [OK]
Common Mistakes:
- Assuming oplog auto-recreates with old data
- Thinking secondaries replicate without oplog
- Confusing oplog deletion with primary election
4. You run
db.oplog.rs.find().limit(1) on a secondary node and get an error. What is the most likely cause?medium
Solution
Step 1: Check database context for oplog
The oplog is stored in thelocaldatabase, so you must switch to it before querying.Step 2: Understand error cause
If you run the command withoutuse local, the shell looks in the wrong database and errors.Final Answer:
You need to switch to the local database first -> Option BQuick Check:
Oplog in local DB = switch DB first [OK]
Hint: Always use 'local' DB to query oplog [OK]
Common Mistakes:
- Trying to query oplog in wrong database
- Assuming oplog only exists on primary
- Thinking oplog is empty on secondaries
5. In a MongoDB replica set, how does the oplog help maintain data consistency during failover when the primary goes down?
hard
Solution
Step 1: Understand failover in replica sets
When primary fails, secondaries elect a new primary to continue operations.Step 2: Role of oplog in failover
Secondaries use oplog entries to apply all changes and ensure they have the latest data before election.Step 3: Why other options are incorrect
Oplog does not merge conflicting writes or prevent writes during failover; it also does not backup externally.Final Answer:
Secondaries use oplog entries to catch up and elect a new primary with latest data -> Option DQuick Check:
Oplog syncs data for failover [OK]
Hint: Oplog helps secondaries catch up before election [OK]
Common Mistakes:
- Thinking oplog merges conflicting writes
- Assuming oplog blocks writes during failover
- Confusing oplog with backup system
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
