📖 Scenario: You are managing a MongoDB database with a replica set. You want to control how your application reads data from the primary and secondary members of the replica set to balance load and ensure data freshness.
🎯 Goal: Build a MongoDB query setup that uses different read preferences to read data from the replica set members.
📋 What You'll Learn
Create a MongoDB collection named products with sample documents.
Set a read preference variable to secondaryPreferred.
Write a query that uses the read preference variable to read documents from the products collection.
Add a final configuration to confirm the read preference is applied.
💡 Why This Matters
🌍 Real World
In real applications, controlling read preference helps balance load between primary and secondary database servers, improving performance and availability.
💼 Career
Database administrators and backend developers often configure read preferences to optimize data reads in distributed MongoDB setups.
Progress0 / 4 steps
1
Create the products collection with sample data
Create a MongoDB collection called products and insert these exact documents: { _id: 1, name: "Pen", price: 1.5 }, { _id: 2, name: "Notebook", price: 3 }, and { _id: 3, name: "Eraser", price: 0.5 }.
MongoDB
Hint
Use db.products.insertMany() with an array of documents.
2
Set the read preference variable
Create a variable called readPref and set it to the string "secondaryPreferred" to specify the read preference.
MongoDB
Hint
Use const readPref = "secondaryPreferred"; to set the variable.
3
Query the products collection using the read preference
Write a query that finds all documents in the products collection using the readPref variable as the read preference option.
MongoDB
Hint
Use db.products.find().readPref(readPref) to apply the read preference.
4
Confirm the read preference is applied
Add a line to assign the read preference readPref to the cursor variable explicitly to confirm the read preference is set.
MongoDB
Hint
Call cursor.readPref() to confirm the read preference is applied.
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
Step 1: Understand the role of readPreference
The readPreference setting tells MongoDB which replica set member to use for read operations.
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.
Final Answer:
Which replica set member to read data from -> Option D
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
Step 1: Identify correct parameter name
The correct parameter to set read preference in the connection string is readPreference.
Step 2: Match correct value
secondaryPreferred is a valid read preference mode; others like primaryOnly or secondaryOnly are invalid or incorrect.
Final Answer:
mongodb://host1,host2/?readPreference=secondaryPreferred -> Option A
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:
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
Step 1: Understand 'secondaryPreferred' behavior
This mode reads from secondary if available; otherwise, it falls back to primary.
Step 2: Reason why primary is used
If no secondary is available or reachable, the driver reads from primary as fallback.
Final Answer:
No secondary members are available, so it falls back to primary -> Option C
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
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.
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.