- Reference >
mongo
Shell Methods >- Connection Methods >
- Mongo.setReadPref()
Mongo.setReadPref()¶
On this page
Definition¶
-
Mongo.
setReadPref
(mode, tagSet)¶ Call the
setReadPref()
method on aMongo
connection object to control how the client will route all queries to members of the replica set. [1]Note
You must call
Mongo.setReadPref()
on the connection object before retrieving documents using that connection to use that read preference.
[1] | To apply a read preference for a specific query or queries, you can
apply cursor.readPref() to a cursor before iteration. See
cursor.readPref() for details. |
Parameters¶
Parameter | Type | Description |
---|---|---|
mode | string | One of the following read preference modes: |
tagSet |
array of documents | Optional. A tag set used to target reads to
members with the specified tag(s). For details, see Read Preference Tag Set Lists. |
Mongo.setReadPref()
does not support the
Read Preference maxStalenessSeconds option for read
preference.
Examples¶
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 target secondaries with specific tags, include the tag set array:
During the secondary selection process, MongoDB tries to find secondary
members with the datacenter: "B"
tag first.
- If found, MongoDB limits the eligible secondaries to those with the
datacenter: "B"
tag and ignores the remaining tags. - If none are found, then, MongoDB tries to find secondary members with
the
"region": "West"
tag.- If found, MongoDB limits the eligible secondaries to those with the
"region": "West"
tag. - If none are found, MongoDB uses any eligible secondaries.
- If found, MongoDB limits the eligible secondaries to those with the
See Order of Tag Matching for details.
See also