This version of the documentation is archived and no longer supported.


On this page


Mongo.setReadPref(mode, tagSet)

Call the setReadPref() method on a Mongo connection object to control how the client will route all queries to members of the replica set.

Parameter Type Description
mode string

One of the following read preference modes: primary, primaryPreferred, secondary, secondaryPreferred, or nearest.

tagSet array Optional. A tag set used to specify custom read preference modes. For details, see Tag Sets.


Specify Read Preference Mode

The following operation sets the read preference mode to target the read to a secondary member. This implicitly allows reads from secondaries.


Specify Read Preference Tag Set

To set a read preference that uses a tag set, specify an array of tag sets as the second argument to Mongo.setReadPref(), as in the following:

db.getMongo().setReadPref('primaryPreferred', [ { "dc": "east" } ] )

You can specify multiple tag sets, in order of preference, as in the following:

                          [ { "dc": "east", "use": "production" },
                            { "dc": "east", "use": "reporting" },
                            { "dc": "east" },
                          ] )

If the replica set cannot satisfy the first tag set, the client will attempt to use the second read preference. Each tag set can contain zero or more field/value tag pairs, with an “empty” document acting as a wildcard which matches a replica set member with any tag set or no tag set.


You must call Mongo.setReadPref() on the connection object before retrieving documents using that connection to use that read preference.