Bird
Raised Fist0
MongoDBquery~10 mins

Read preference for replica sets in MongoDB - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the read preference to primary in a MongoDB client connection.

MongoDB
const client = new MongoClient(uri, { readPreference: '[1]' });
Drag options to blanks, or click blank then click option'
AprimaryPreferred
Bsecondary
Cprimary
Dnearest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'secondary' when you want to read from primary.
Confusing 'primaryPreferred' with 'primary'.
2fill in blank
medium

Complete the code to set the read preference to read from secondary members only.

MongoDB
const client = new MongoClient(uri, { readPreference: '[1]' });
Drag options to blanks, or click blank then click option'
Asecondary
Bprimary
CprimaryPreferred
Dnearest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'primary' or 'primaryPreferred' instead of 'secondary'.
Confusing 'nearest' with 'secondary'.
3fill in blank
hard

Fix the error in the code to set the read preference to 'nearest'.

MongoDB
const client = new MongoClient(uri, { readPreference: [1] });
Drag options to blanks, or click blank then click option'
A'nearest'
Bnearest
C"nearest"
D'secondary'
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the string value causes a syntax error.
Using the wrong read preference string.
4fill in blank
hard

Fill both blanks to create a MongoDB client that reads from secondary members but falls back to primary if no secondary is available.

MongoDB
const client = new MongoClient(uri, { readPreference: '[1]', readPreferenceTags: [ { '[2]': '' } ] });
Drag options to blanks, or click blank then click option'
AsecondaryPreferred
Bsecondary
Cprimary
Ddc
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'secondary' instead of 'secondaryPreferred' causes no fallback.
Using incorrect tag keys in readPreferenceTags.
5fill in blank
hard

Fill all three blanks to create a MongoDB client that reads from the nearest member in the 'east' data center with a max staleness of 120 seconds.

MongoDB
const client = new MongoClient(uri, { readPreference: '[1]', readPreferenceTags: [ { '[2]': '[3]' } ], maxStalenessSeconds: 120 });
Drag options to blanks, or click blank then click option'
Anearest
Bdc
Ceast
Dsecondary
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'secondary' instead of 'nearest' changes read behavior.
Incorrect tag keys or values cause no filtering.

Practice

(1/5)
1. What does the readPreference setting control in a MongoDB replica set?
easy
A. The size of the replica set
B. Which member to write data to
C. How often data is backed up
D. Which replica set member to read data from

Solution

  1. Step 1: Understand the role of readPreference

    The readPreference setting tells MongoDB which replica set member to use for read operations.
  2. Step 2: Differentiate read and write roles

    Writes always go to the primary, but reads can be directed to primary or secondary members based on readPreference.
  3. Final Answer:

    Which replica set member to read data from -> Option D
  4. Quick Check:

    Read preference = controls read source [OK]
Hint: Read preference decides read source, not write or backup [OK]
Common Mistakes:
  • Confusing readPreference with write concern
  • Thinking it controls backups
  • Assuming it changes replica set size
2. Which of the following is the correct syntax to set read preference to secondaryPreferred in a MongoDB connection string?
easy
A. mongodb://host1,host2/?readPreference=secondaryPreferred
B. mongodb://host1,host2/?readPreference=primaryOnly
C. mongodb://host1,host2/?readPref=secondaryPreferred
D. mongodb://host1,host2/?readPreference=secondaryOnly

Solution

  1. Step 1: Identify correct parameter name

    The correct parameter to set read preference in the connection string is readPreference.
  2. Step 2: Match correct value

    secondaryPreferred is a valid read preference mode; others like primaryOnly or secondaryOnly are invalid or incorrect.
  3. Final Answer:

    mongodb://host1,host2/?readPreference=secondaryPreferred -> Option A
  4. Quick Check:

    Use readPreference with valid mode [OK]
Hint: Use exact parameter name 'readPreference' and valid mode [OK]
Common Mistakes:
  • Using 'readPref' instead of 'readPreference'
  • Using invalid modes like 'primaryOnly'
  • Misspelling 'secondaryPreferred'
3. Given the following MongoDB query with read preference set to secondary:
db.collection.find({ status: "active" }).readPref("secondary")
What will happen if all secondary members are down?
medium
A. The query will read from the primary member
B. The query will fail with an error
C. The query will wait until a secondary is available
D. The query will return empty results

Solution

  1. Step 1: Understand 'secondary' read preference behavior

    When read preference is set to secondary, reads must come from secondary members only.
  2. Step 2: Consider all secondaries down scenario

    If no secondary is available, the query cannot read from primary or fallback; it results in an error.
  3. Final Answer:

    The query will fail with an error -> Option B
  4. Quick Check:

    Strict 'secondary' fails if no secondary [OK]
Hint: Strict 'secondary' read fails if no secondary available [OK]
Common Mistakes:
  • Assuming fallback to primary happens automatically
  • Thinking query waits indefinitely
  • Expecting empty results instead of error
4. You wrote this code to read from a secondary member:
db.collection.find().readPref('secondaryPreferred')
But it still reads from the primary. What is the likely cause?
medium
A. The query must specify a filter to use read preference
B. The syntax for setting read preference is incorrect
C. No secondary members are available, so it falls back to primary
D. Read preference only works for writes

Solution

  1. Step 1: Understand 'secondaryPreferred' behavior

    This mode reads from secondary if available; otherwise, it falls back to primary.
  2. Step 2: Reason why primary is used

    If no secondary is available or reachable, the driver reads from primary as fallback.
  3. Final Answer:

    No secondary members are available, so it falls back to primary -> Option C
  4. Quick Check:

    'secondaryPreferred' falls back to primary if no secondary [OK]
Hint: 'secondaryPreferred' falls back to primary if no secondary [OK]
Common Mistakes:
  • Thinking syntax is wrong when it is correct
  • Believing read preference needs a filter
  • Confusing read preference with write behavior
5. You want to optimize read performance by reading from secondaries but ensure data is fresh enough. Which read preference mode should you choose?
hard
A. secondaryPreferred
B. nearest
C. primary
D. secondary

Solution

  1. Step 1: Understand read preference modes

    primary reads only from primary (fresh but may be slower), secondary reads only from secondaries (may be stale), secondaryPreferred reads secondaries or falls back to primary, nearest reads from the closest member regardless of type.
  2. Step 2: Balance freshness and performance

    secondaryPreferred prefers secondaries for read performance optimization but falls back to the primary to ensure data freshness and availability.
  3. Final Answer:

    secondaryPreferred -> Option A
  4. Quick Check:

    secondaryPreferred prefers secondary perf + primary freshness fallback [OK]
Hint: 'secondaryPreferred' for secondary perf with primary fallback [OK]
Common Mistakes:
  • Choosing 'secondary' which may return stale data
  • Choosing 'primary' which may be slower
  • Confusing 'secondaryPreferred' with 'nearest'
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