@@ -45,6 +45,11 @@ in your replica sets.
45
45
Configure Read and Write Operations
46
46
-----------------------------------
47
47
48
+ You can control how the library routes read operations by setting a **read preference**.
49
+ You can also control options for how the library waits for acknowledgment of
50
+ read and write operations on a replica set by setting a **read concern** and a
51
+ **write concern**.
52
+
48
53
This section shows how to configure the read preference, read concern, and write
49
54
concern at various levels by passing an options array parameter to the following
50
55
methods:
@@ -84,9 +89,17 @@ In the options parameter, specify the following values:
84
89
Client Configuration Example
85
90
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86
91
87
- The following example shows how to set the read preference, read concern, and
92
+ This example shows how to set the read preference, read concern, and
88
93
write concern of a ``MongoDB\Client`` instance by passing an array to
89
- the constructor:
94
+ the constructor. The code configures the following settings:
95
+
96
+ - ``secondary`` read preference: Read operations retrieve data from
97
+ secondary replica set members
98
+ - ``local`` read concern: Read operations return the instance's most recent data
99
+ without guaranteeing that the data has been written to a majority of the replica
100
+ set members
101
+ - ``2`` write concern: The primary and one secondary replica set member
102
+ must acknowledge the write operation
90
103
91
104
.. literalinclude:: /includes/read-write-pref.php
92
105
:language: php
@@ -108,9 +121,18 @@ URI, which is passed as a parameter to the ``MongoDB\Client`` constructor:
108
121
Session Configuration Example
109
122
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110
123
111
- The following example shows how to set the read preference, read concern, and
124
+ This example shows how to set the read preference, read concern, and
112
125
write concern of a session by passing an array to the ``startSession()``
113
- method:
126
+ method. The code configures the following settings:
127
+
128
+ - ``PRIMARY_PREFERRED`` read preference: Read operations retrieve data from
129
+ the primary replica set member, or secondary members if the primary is unavailable
130
+ - ``LOCAL`` read concern: Read operations return the instance's most recent data
131
+ without guaranteeing that the data has been written to a majority of the replica
132
+ set members
133
+ - ``MAJORITY`` write concern: The majority of all replica set members
134
+ must acknowledge the write operation
135
+
114
136
115
137
.. literalinclude:: /includes/read-write-pref.php
116
138
:language: php
@@ -123,9 +145,16 @@ method:
123
145
Transaction Configuration Example
124
146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125
147
126
- The following example shows how to set the read preference, read concern, and
148
+ This example shows how to set the read preference, read concern, and
127
149
write concern of a transaction by passing an array to the ``startTransaction()``
128
- method:
150
+ method. The code configures the following settings:
151
+
152
+ - ``PRIMARY`` read preference: Read operations retrieve data from
153
+ the primary replica set member
154
+ - ``MAJORITY`` read concern: Read operations return the instance's most recent data
155
+ that has been written to a majority of replica set members
156
+ - ``1`` write concern: The primary replica set member must acknowledge the
157
+ write operation
129
158
130
159
.. literalinclude:: /includes/read-write-pref.php
131
160
:language: php
@@ -138,9 +167,17 @@ method:
138
167
Database Configuration Example
139
168
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
140
169
141
- The following example shows how to set the read preference, read concern, and
170
+ This example shows how to set the read preference, read concern, and
142
171
write concern of a database called ``test_database`` by passing an options
143
- array to the ``selectDatabase()`` method:
172
+ array to the ``selectDatabase()`` method. The code configures the following settings:
173
+
174
+ - ``PRIMARY_PREFERRED`` read preference: Read operations retrieve data from
175
+ the primary replica set member, or secondary members if the primary is unavailable
176
+ - ``AVAILABLE`` read concern: Read operations return the instance's most recent data
177
+ without guaranteeing that the data has been written to a majority of the replica
178
+ set members
179
+ - ``MAJORITY`` write concern: The majority of all replica set members
180
+ must acknowledge the write operation
144
181
145
182
.. literalinclude:: /includes/databases-collections/databases-collections.php
146
183
:language: php
@@ -153,9 +190,16 @@ array to the ``selectDatabase()`` method:
153
190
Collection Configuration Example
154
191
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
155
192
156
- The following example shows how to set the read preference, read concern, and
193
+ This example shows how to set the read preference, read concern, and
157
194
write concern of a collection called ``test_collection`` by passing an options
158
- array to the ``selectCollection()`` method:
195
+ array to the ``selectCollection()`` method. The code configures the following settings:
196
+
197
+ - ``SECONDARY_PREFERRED`` read preference: Read operations retrieve data from
198
+ secondary replica set members, or the primary members if no secondaries are available
199
+ - ``AVAILABLE`` read concern: Read operations return the instance's most recent data
200
+ without guaranteeing that the data has been written to a majority of the replica
201
+ set members
202
+ - ``0`` write concern: Requests no acknowledgment of the write operation
159
203
160
204
.. literalinclude:: /includes/databases-collections/databases-collections.php
161
205
:language: php
0 commit comments