Complete the code to set the read preference to primary in a MongoDB client connection.
const client = new MongoClient(uri, { readPreference: '[1]' });The primary read preference directs all reads to the primary member of the replica set.
Complete the code to set the read preference to read from secondary members only.
const client = new MongoClient(uri, { readPreference: '[1]' });The secondary read preference directs reads only to secondary members of the replica set.
Fix the error in the code to set the read preference to 'nearest'.
const client = new MongoClient(uri, { readPreference: [1] });The readPreference value must be a string, so it needs quotes around it.
Fill both blanks to create a MongoDB client that reads from secondary members but falls back to primary if no secondary is available.
const client = new MongoClient(uri, { readPreference: '[1]', readPreferenceTags: [ { '[2]': '' } ] });secondaryPreferred reads from secondaries but falls back to primary if none are available. The dc tag is commonly used to specify a data center for read preference tags.
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.
const client = new MongoClient(uri, { readPreference: '[1]', readPreferenceTags: [ { '[2]': '[3]' } ], maxStalenessSeconds: 120 });nearest reads from the closest member regardless of primary or secondary. The dc tag filters members in the 'east' data center. maxStalenessSeconds limits how stale the data can be.