Ganesan G
Ganesan G

Reputation: 69

How to connect specific secondary node in MongoDB Replica Set

I configured cluster with 2 secondary node. So, totally 3 node(1 primary, 2 secondary). My intention is to dedicate one of the secondary node to an application for read only. But, i could not find the connection property to achieve this. Can anyone help me on this.

Upvotes: 2

Views: 3498

Answers (2)

drgnfr
drgnfr

Reputation: 131

For redirecting read operations to the secondary node(s), specify readPreference connection option as either secondary or secondaryPreferred.
(Ref. https://www.mongodb.com/docs/manual/core/read-preference/#read-preference-modes-1)

However, to answer precisely your question, as to how to connect to a specific secondary node, do that with the directConnection option.
E.g.

mongodb://<credentials>@node2.cluster.com/?directConnection=true

This is useful for e.g. diagnostic reasons.

Upvotes: 2

ROHIT KHURANA
ROHIT KHURANA

Reputation: 983

By setting secondaryPreferred as the read preference.

The documentation statues for secondaryPreferred in most situations, operations read from secondary members but if no secondary members are available, operations read from the primary

Note: You can tune your secondary read preference by configuring maxStaleTimeout

Please follow below link for more documentation and understanding about read preference https://docs.mongodb.com/manual/core/read-preference/

Upvotes: 1

Related Questions