0
0
MongoDBquery~10 mins

Read preference for replica sets in MongoDB - Interactive Code Practice

Choose your learning style9 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.