-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcursor.readPref.txt
132 lines (84 loc) · 3.26 KB
/
cursor.readPref.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
=================
cursor.readPref()
=================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: cursor.readPref(mode, tagSet)
.. include:: /includes/fact-mongosh-shell-method.rst
Append :method:`~cursor.readPref()` to a cursor to
control how the client routes the query to members
of the replica set.
.. note::
You must apply :method:`~cursor.readPref()` to the cursor before retrieving
any documents from the database.
Parameters
~~~~~~~~~~
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - :ref:`mode <cursor-readpref-mode>`
- string
- .. _cursor-readpref-mode:
One of the following :term:`read preference` modes: :readmode:`primary`,
:readmode:`primaryPreferred`, :readmode:`secondary`,
:readmode:`secondaryPreferred`, or :readmode:`nearest`
* - :ref:`tagSet <cursor-readpref-tagset>`
- array of documents
- .. _cursor-readpref-tagset:
Optional. A :ref:`tag set
<replica-set-read-preference-tag-sets>` used to target reads to
members with the specified tag(s). ``tagSet`` is not available
if using :readmode:`primary`.
For details, see :ref:`replica-set-read-preference-tag-sets`.
:method:`~cursor.readPref()` does not support the
:ref:`replica-set-read-preference-max-staleness` option for read
preference.
Compatibility
-------------
This method is available in deployments hosted in the following environments:
.. include:: /includes/fact-environments-atlas-only.rst
.. include:: /includes/fact-environments-atlas-support-all.rst
.. include:: /includes/fact-environments-onprem-only.rst
Examples
--------
Specify Read Preference Mode
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following operation uses the read preference :ref:`mode
<cursor-readpref-mode>` to target the read to a secondary member.
.. code-block:: javascript
db.collection.find({ }).readPref( "secondary")
Specify Read Preference Tag Set
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To target secondaries with specific tags, include both the :ref:`mode
<cursor-readpref-mode>` and the :ref:`tagSet <cursor-readpref-tagset>`
array:
.. code-block:: javascript
db.collection.find({ }).readPref(
"secondary",
[
{ "datacenter": "B" }, // First, try matching by the datacenter tag
{ "region": "West"}, // If not found, then try matching by the region tag
{ } // If not found, then use the empty document to match all eligible members
]
)
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.
See :ref:`read-pref-order-matching` for details.
.. seealso::
:doc:`/tutorial/configure-replica-set-tag-sets`