MongoDB Read Preference

Intermediate
7 min read
Updated: Nov 2026
Getting Started

What Is Read Preference?

In a replica set, one member is the primary and the others are secondaries that replicate from it. A read preference tells the client which member to read from. The default is primary, which always gives the latest committed writes. Reading from secondaries can spread load or reduce latency, but secondary data may be slightly behind (replication lag).

Why It Matters

ReasonBenefit
Scale read trafficAnalytics or reporting can read from secondaries and leave the primary free for writes.
Lower latencynearest can serve users from the closest data center.
Availability optionsPreferred modes keep reads working during a primary failover.
Control over freshnessprimary gives the freshest data; others trade freshness for capacity.

How To Use It

  1. Connect to a replica set, for example an Atlas cluster or a local replica set.
  2. Run rs.status() to identify the primary and secondaries.
  3. Run a read with the default (primary) mode and note it succeeds.
  4. Switch the cursor to a secondary with .readPref("secondary").
  5. Set the mode for a whole connection using readPreference in the URI.
  6. Consider maxStalenessSeconds when secondaries must be reasonably fresh.
  7. Decide per feature: critical, read-your-own-write reads use primary; reports can use a secondary.

mongosh Examples

mongosh
// Which members exist?
rs.status().members.map(m => ({ name: m.name, state: m.stateStr }))

// Default: reads from the primary
db.orders.find({ status: "new" })

// Read from a secondary for this query
db.orders.find({ status: "shipped" }).readPref("secondary")

// Prefer secondaries, fall back to primary
db.orders.find().readPref("secondaryPreferred")

// Lowest latency member
db.orders.find().readPref("nearest")

// As a connection string option
// mongodb://host1,host2,host3/shop?replicaSet=rs0&readPreference=secondaryPreferred&maxStalenessSeconds=90

Edge Cases & Pitfalls

Stale reads from secondaries

A document you just wrote may not yet be on a secondary. Do not read from secondaries right after writes when you need that data.

Does not scale writes

Read preference only moves reads. All writes still go to the primary.

secondary fails without secondaries

With mode secondary and none available, the read errors, unlike secondaryPreferred.

Uneven load

Pointing all reads at one secondary can overload it. Watch lag and resource usage.

Cheat Sheet

TopicDetail
primaryDefault. All reads go to the primary; errors if there is no primary
primaryPreferredPrimary if available, otherwise a secondary
secondaryReads only from secondaries
secondaryPreferredSecondary if available, otherwise the primary
nearestMember with the lowest network latency, primary or secondary
maxStalenessSecondsAvoid secondaries that lag more than this many seconds
Tag setsTarget members with specific tags, such as a data center
cursor.readPref()Set the mode for a cursor in mongosh
URI option?readPreference=secondaryPreferred in the connection string
WritesAlways go to the primary, regardless of read preference

Key Takeaways

Read preference decides where a read goes. Read concern decides what data the read may see. Use primary unless you accept possibly stale data.

Frequently Asked Questions

What is the default read preference?

primary. All reads go to the primary member.

Can I read from secondaries?

Yes, by using secondary, secondaryPreferred, or nearest. Data may lag slightly behind the primary.

What is the difference between secondary and secondaryPreferred?

secondary errors if no secondary is available. secondaryPreferred falls back to the primary.

What does nearest do?

It picks the member with the lowest network latency, whether it is the primary or a secondary.

Does read preference affect writes?

No. Writes always go to the primary.

How is read preference different from read concern?

Read preference selects which member serves the read. Read concern sets the consistency and durability level of the data returned.

Next: MongoDB Read Concern

Continue the MongoDB learning path.

MongoDB Read Concern →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful