Bird
Raised Fist0
MongoDBquery~20 mins

Primary and secondary nodes in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Replica Set Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Role of Primary Node in MongoDB Replica Set

In a MongoDB replica set, what is the main responsibility of the primary node?

AIt balances the load by distributing read requests among all nodes.
BIt only stores backup data and does not accept any client requests.
CIt handles all write operations and replicates data to secondary nodes.
DIt monitors network traffic but does not store any data.
Attempts:
2 left
💡 Hint

Think about which node accepts changes and shares them with others.

❓ query_result
intermediate
2:00remaining
Identifying the Primary Node with rs.status()

Given the output of rs.status() command in MongoDB, which field indicates the primary node?

MongoDB
rs.status()
AThe member with "stateStr" value "PRIMARY"
BThe member with "health" value 0
CThe member with "stateStr" value "SECONDARY"
DThe member with "optimeDate" set to null
Attempts:
2 left
💡 Hint

Look for the status string that explicitly says which node is primary.

📝 Syntax
advanced
2:00remaining
Correct Command to Step Down Primary Node

Which MongoDB shell command correctly forces the current primary node to step down and triggers an election?

Ars.forceStepDown(60)
Brs.stepdown(60)
Crs.step_down(60)
Drs.stepDown(60)
Attempts:
2 left
💡 Hint

Check the exact spelling and camel case of the command.

❓ optimization
advanced
2:00remaining
Optimizing Read Preference for Secondary Nodes

You want to distribute read operations to secondary nodes to reduce load on the primary. Which read preference setting achieves this in MongoDB?

A"primaryOnly"
B"secondaryPreferred"
C"nearest"
D"primaryPreferred"
Attempts:
2 left
💡 Hint

Think about a setting that prefers secondaries but falls back to primary if needed.

🔧 Debug
expert
3:00remaining
Diagnosing Replica Set Election Issue

A MongoDB replica set has three nodes: one primary and two secondaries. Suddenly, the primary becomes unreachable. However, no new primary is elected. What is the most likely cause?

AThe majority of nodes are not available, so election cannot proceed.
BThe secondary nodes have higher priority than the primary.
CThe replica set configuration has no arbiter node.
DThe primary node was manually forced to step down without election enabled.
Attempts:
2 left
💡 Hint

Consider how MongoDB decides to elect a new primary based on node availability.

Practice

(1/5)
1. Which statement best describes the role of a primary node in a MongoDB replica set?
easy
A. It acts as a backup and does not participate in replication.
B. It only reads data and never accepts writes.
C. It accepts write operations and replicates data to secondary nodes.
D. It is used only for analytics and reporting.

Solution

  1. Step 1: Understand the primary node's function

    The primary node is the main server that accepts all write operations in a replica set.
  2. Step 2: Role in replication

    The primary replicates data changes to secondary nodes to keep data consistent.
  3. Final Answer:

    It accepts write operations and replicates data to secondary nodes. -> Option C
  4. Quick Check:

    Primary node = accepts writes and replicates [OK]
Hint: Primary node handles writes; secondaries replicate [OK]
Common Mistakes:
  • Thinking primary only reads data
  • Confusing secondary with primary roles
  • Assuming primary is just a backup
2. Which of the following is the correct command to check the status of a MongoDB replica set?
easy
A. replica.status()
B. db.replicaStatus()
C. show replicaSet
D. rs.status()

Solution

  1. Step 1: Recall MongoDB replica set commands

    The command rs.status() is used to check the status of the replica set.
  2. Step 2: Verify other options

    Other options are not valid MongoDB shell commands for replica set status.
  3. Final Answer:

    rs.status() -> Option D
  4. Quick Check:

    Replica set status command = rs.status() [OK]
Hint: Use rs.status() to check replica set health [OK]
Common Mistakes:
  • Using incorrect or non-existent commands
  • Confusing database commands with replica set commands
  • Typing commands with wrong syntax
3. Given a replica set with one primary and two secondary nodes, what will happen if the primary node goes down?
medium
A. The replica set will stop all read and write operations immediately.
B. One of the secondary nodes will be elected as the new primary.
C. The primary node will automatically restart without intervention.
D. All secondary nodes will become primary nodes simultaneously.

Solution

  1. Step 1: Understand replica set failover

    If the primary node fails, the replica set automatically elects a new primary from the secondaries.
  2. Step 2: Check other options

    The replica set does not stop all operations; it does not restart primary automatically; multiple primaries cannot exist simultaneously.
  3. Final Answer:

    One of the secondary nodes will be elected as the new primary. -> Option B
  4. Quick Check:

    Failover elects new primary from secondaries [OK]
Hint: Replica sets auto-elect new primary if current fails [OK]
Common Mistakes:
  • Thinking all nodes become primary
  • Assuming system stops all operations
  • Believing primary restarts automatically
4. You run rs.stepDown() on the primary node but get an error saying "not primary". What is the likely cause?
medium
A. The node is already a secondary, not primary.
B. The command syntax is incorrect.
C. The replica set has only one node.
D. The network connection to the primary is lost.

Solution

  1. Step 1: Understand rs.stepDown() usage

    This command only works on a primary node to force it to step down.
  2. Step 2: Analyze the error message

    "not primary" means the node is already a secondary, so it cannot step down.
  3. Final Answer:

    The node is already a secondary, not primary. -> Option A
  4. Quick Check:

    rs.stepDown() error "not primary" = node is secondary [OK]
Hint: rs.stepDown() only works on primary nodes [OK]
Common Mistakes:
  • Trying to step down a secondary node
  • Assuming syntax error causes this message
  • Ignoring the node's current role
5. You want to configure a MongoDB replica set with one primary and two secondaries. Which of the following configurations ensures data safety and availability during a primary failure?
hard
A. Set all nodes with priority 1 and enable majority write concern.
B. Set one secondary with priority 0 and disable write concern.
C. Use only one node as primary without secondaries.
D. Set all nodes with priority 0 to prevent elections.

Solution

  1. Step 1: Understand priority and write concern

    Setting all nodes with priority 1 allows any secondary to become primary if needed. Majority write concern ensures writes are acknowledged by most nodes.
  2. Step 2: Evaluate other options

    Priority 0 disables election for that node; disabling write concern risks data loss; single node has no failover; priority 0 on all nodes prevents elections.
  3. Final Answer:

    Set all nodes with priority 1 and enable majority write concern. -> Option A
  4. Quick Check:

    Priority 1 + majority write concern = safe failover [OK]
Hint: Use priority 1 and majority write concern for safety [OK]
Common Mistakes:
  • Disabling write concern risking data loss
  • Setting priority 0 disables elections
  • Using single node without replicas
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard