OPTIONS

Mongo.setReadPref()

On this page

Definition

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.

Examples

To set a read preference mode in the mongo shell, use the following operation:

db.getMongo().setReadPref('primaryPreferred')

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:

db.getMongo().setReadPref('secondaryPreferred',
                          [ { "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.

Note

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