Bird
Raised Fist0
MongoDBquery~20 mins

Replica set configuration basics 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!
❓ query_result
intermediate
2:00remaining
Check the primary member of a replica set
Given the following command run on a MongoDB replica set, what is the output field that indicates the current primary member's name?
MongoDB
rs.status()
AThe field 'members' with a subfield 'stateStr' equal to 'PRIMARY' shows the primary member's name.
BThe field 'config' contains the primary member's name.
CThe field 'set' contains the primary member's name.
DThe field 'primary' directly contains the hostname of the primary member.
Attempts:
2 left
💡 Hint
Look for a field that directly names the primary member in the replica set status output.
🧠 Conceptual
intermediate
2:00remaining
Understanding voting members in a replica set
In a MongoDB replica set configuration, what is the maximum number of voting members allowed to ensure proper election and fault tolerance?
A3 voting members maximum
B5 voting members maximum
C7 voting members maximum
D9 voting members maximum
Attempts:
2 left
💡 Hint
MongoDB limits voting members to a certain number to maintain election stability.
📝 Syntax
advanced
2:00remaining
Identify the correct syntax to add a new member to a replica set
Which of the following commands correctly adds a new member with hostname 'mongo3.example.net:27017' to an existing replica set configuration?
MongoDB
var cfg = rs.conf();
cfg.members.push({ _id: 2, host: 'mongo3.example.net:27017' });
rs.reconfig(cfg);
Avar cfg = rs.conf(); cfg.members.push({ _id: 2, host: 'mongo3.example.net:27017' }); rs.reconfig(cfg);
Bvar cfg = rs.status(); cfg.members.push({ _id: 2, host: 'mongo3.example.net:27017' }); rs.reconfig(cfg);
Cvar cfg = rs.conf(); cfg.members.add({ _id: 2, host: 'mongo3.example.net:27017' }); rs.reconfig(cfg);
Dvar cfg = rs.conf(); cfg.push({ _id: 2, host: 'mongo3.example.net:27017' }); rs.reconfig(cfg);
Attempts:
2 left
💡 Hint
Check the method used to get the current config and how to add a member to the members array.
🔧 Debug
advanced
2:00remaining
Diagnose the error when reconfiguring a replica set
A user runs the following commands to add a new member but gets an error: 'reconfig failed: new configuration must have a unique _id for each member'. What is the likely cause?
MongoDB
var cfg = rs.conf();
cfg.members.push({ _id: 1, host: 'mongo3.example.net:27017' });
rs.reconfig(cfg);
AThe rs.reconfig() command must be run with force:true option.
BThe host field is incorrect and must include the replica set name.
CThe configuration object must be cloned before modification.
DThe _id 1 is already used by an existing member, causing a duplicate _id error.
Attempts:
2 left
💡 Hint
Check the uniqueness of the _id field for each member in the config.
🧠 Conceptual
expert
2:00remaining
Understanding priority and elections in replica sets
In a MongoDB replica set, what effect does setting a member's priority to 0 have on elections?
AThe member cannot vote or become primary.
BThe member cannot become primary but can vote in elections.
CThe member becomes the highest priority for primary election.
DThe member is removed from the replica set.
Attempts:
2 left
💡 Hint
Priority controls eligibility to become primary, voting controls election votes.

Practice

(1/5)
1. What is the main purpose of a MongoDB replica set?
easy
A. To keep multiple copies of data for safety and availability
B. To speed up queries by using multiple CPUs
C. To store data in different formats
D. To backup data to an external server

Solution

  1. Step 1: Understand what a replica set is

    A replica set is a group of MongoDB servers that keep copies of the same data.
  2. Step 2: Identify the main goal of this setup

    The main goal is to keep data safe and available by having multiple copies.
  3. Final Answer:

    To keep multiple copies of data for safety and availability -> Option A
  4. Quick Check:

    Replica set purpose = data safety and availability [OK]
Hint: Replica sets keep data copies for safety and uptime [OK]
Common Mistakes:
  • Thinking replica sets speed up queries directly
  • Confusing replica sets with backups
  • Assuming replica sets change data formats
2. Which command correctly initiates a replica set with one member named 'mongo1'?
easy
A. rs.start({ _id: 'rs0', members: ['mongo1'] })
B. rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongo1:27017' }] })
C. replicaSet.create({ name: 'rs0', nodes: ['mongo1'] })
D. initReplicaSet({ id: 'rs0', hosts: ['mongo1:27017'] })

Solution

  1. Step 1: Recall the correct syntax for initiating a replica set

    The command is rs.initiate() with a config object including _id and members array.
  2. Step 2: Check the options for correct syntax and fields

    rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongo1:27017' }] }) uses rs.initiate with _id and members array with _id and host fields correctly.
  3. Final Answer:

    rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongo1:27017' }] }) -> Option B
  4. Quick Check:

    rs.initiate with config object = correct syntax [OK]
Hint: Use rs.initiate with _id and members array for setup [OK]
Common Mistakes:
  • Using wrong command names like rs.start or initReplicaSet
  • Passing members as simple strings instead of objects
  • Missing port number in host field
3. Given this replica set config:
rs.initiate({ _id: 'rs0', members: [ { _id: 0, host: 'mongo1:27017' }, { _id: 1, host: 'mongo2:27017' } ] })

What will be the state of the second member immediately after initiation?
medium
A. ARBITER
B. SECONDARY
C. PRIMARY
D. STARTUP

Solution

  1. Step 1: Understand member states after rs.initiate()

    After initiation, members start in STARTUP state before election completes.
  2. Step 2: Identify the initial state of the second member

    The second member will be in STARTUP state until it syncs and elections finish.
  3. Final Answer:

    STARTUP -> Option D
  4. Quick Check:

    New members start as STARTUP before becoming SECONDARY [OK]
Hint: New members start in STARTUP state before election [OK]
Common Mistakes:
  • Assuming member is immediately PRIMARY or SECONDARY
  • Confusing ARBITER with normal members
  • Ignoring the startup phase after initiation
4. You run rs.initiate() but get an error: "members array is missing or empty." What is the likely cause?
medium
A. You did not provide a members array in the config object
B. You used the wrong command name
C. You included an invalid port number
D. You forgot to start the mongod service

Solution

  1. Step 1: Analyze the error message

    The error says the members array is missing or empty, meaning the config lacks members.
  2. Step 2: Match error to common causes

    Not providing the members array in rs.initiate config causes this error.
  3. Final Answer:

    You did not provide a members array in the config object -> Option A
  4. Quick Check:

    Missing members array = error message [OK]
Hint: Always include members array in rs.initiate config [OK]
Common Mistakes:
  • Ignoring the need for members array
  • Assuming wrong command name causes this error
  • Thinking service not started causes this error
5. You want to add a third member as an arbiter to your replica set. Which configuration snippet correctly adds it?
hard
A. { _id: 2, host: 'mongo3:27017', votes: 0 }
B. { _id: 2, host: 'mongo3:27017', priority: 2 }
C. { _id: 2, host: 'mongo3:27017', arbiterOnly: true }
D. { _id: 2, host: 'mongo3:27017', hidden: true }

Solution

  1. Step 1: Understand arbiter configuration

    An arbiter member is configured with the field arbiterOnly set to true.
  2. Step 2: Identify the correct option

    { _id: 2, host: 'mongo3:27017', arbiterOnly: true } sets arbiterOnly: true, which correctly marks the member as an arbiter.
  3. Final Answer:

    { _id: 2, host: 'mongo3:27017', arbiterOnly: true } -> Option C
  4. Quick Check:

    ArbiterOnly true = arbiter member [OK]
Hint: Use arbiterOnly: true to add an arbiter member [OK]
Common Mistakes:
  • Using priority or votes fields incorrectly for arbiters
  • Confusing hidden members with arbiters
  • Not setting arbiterOnly to true for arbiters
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