Skip to content

Commit 16de291

Browse files
authored
DOCSP-41965: Read and write settings (#146)
* DOCSP-41965: Read and write settings * more info * edits * edits * headers * fix link * fix * RR feedback * api docs * fix * fix
1 parent 6f5cdc7 commit 16de291

File tree

4 files changed

+386
-112
lines changed

4 files changed

+386
-112
lines changed

source/databases-collections.txt

Lines changed: 4 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,8 @@ The following example deletes the ``test_collection`` collection:
193193
Configure Read and Write Operations
194194
-----------------------------------
195195

196-
You can control how the library routes read operations by setting a **read preference**.
197-
You can also control options for how the library waits for acknowledgment of
198-
read and write operations on a replica set by setting a **read concern** and a
199-
**write concern**.
196+
You can control how read and write operations run on replica sets
197+
by specifying a read preference, read concern, or write concern.
200198

201199
By default, databases inherit read and write settings from the ``MongoDB\Client``
202200
instance. Collections inherit these settings from the ``MongoDB\Client`` or
@@ -205,114 +203,8 @@ You can change these settings by passing an options array to the
205203
``MongoDB\Client::selectDatabase()``, ``MongoDB\Client::selectCollection()``, or
206204
``MongoDB\Database::selectCollection()`` methods.
207205

208-
To change the read preference, read concern, and write concern of your database
209-
or collection, set the following options in the array parameter:
210-
211-
- ``readPreference``: Sets the read preference. For a list of available read
212-
preferences, see :php:`MongoDB\Driver\ReadPreference <mongodb-driver-readpreference>`
213-
in the extension API documentation.
214-
- ``readConcern``: Sets the read concern. For a list of available read
215-
concerns, see :php:`MongoDB\Driver\ReadConcern <mongodb-driver-readconcern>`
216-
in the extension API documentation.
217-
- ``writeConcern``: Sets the write concern. For a list of available write
218-
concerns, see :php:`MongoDB\Driver\WriteConcern <mongodb-driver-writeconcern>`
219-
in the extension API documentation.
220-
221-
.. tip::
222-
223-
To learn more about read preferences and read and write concerns, see the
224-
following guides in the MongoDB Server manual:
225-
226-
- :manual:`Read Preference </core/read-preference/>`
227-
- :manual:`Read Concern </reference/read-concern/>`
228-
- :manual:`Write Concern </reference/write-concern/>`
229-
230-
Database Configuration Example
231-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
232-
233-
The following example shows how to set the read preference, read concern, and
234-
write concern of a database called ``test_database`` by passing an options
235-
array to ``selectDatabase()``:
236-
237-
.. literalinclude:: /includes/databases-collections/databases-collections.php
238-
:language: php
239-
:dedent:
240-
:start-after: start-database-settings
241-
:end-before: end-database-settings
242-
243-
Collection Configuration Example
244-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245-
246-
The following example shows how to set the read preference, read concern, and
247-
write concern of a collection called ``test_collection`` by passing an options
248-
array to ``selectCollection()``:
249-
250-
.. literalinclude:: /includes/databases-collections/databases-collections.php
251-
:language: php
252-
:dedent:
253-
:start-after: start-collection-settings
254-
:end-before: end-collection-settings
255-
256-
Advanced Read Configurations
257-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
258-
259-
Tag Sets
260-
````````
261-
262-
In {+mdb-server+}, you can apply key-value :manual:`tags
263-
</core/read-preference-tags/>` to replica-set
264-
members according to any criteria you choose. You can then use
265-
those tags to target one or more members for a read operation.
266-
267-
By default, the {+php-library+} ignores tags when choosing a member
268-
to read from. To instruct the {+php-library+} to prefer certain tags,
269-
pass them as a parameter to your ``MongoDB\Driver\ReadPreference`` class
270-
constructor. Then, set the ``MongoDB\Driver\ReadPreference`` object as
271-
the value of the ``readPreference`` database option.
272-
273-
This code example sets the ``readPreference`` option to a tag set
274-
that instructs ``test_database`` to prefer reads from secondary replica set
275-
members in the following order:
276-
277-
1. Members from the New York data center (``['dc' => 'ny']``)
278-
#. Members from the San Francisco data center (``['dc' => 'sf']``)
279-
#. Any secondary members (``[]``)
280-
281-
.. literalinclude:: /includes/databases-collections/databases-collections.php
282-
:language: php
283-
:dedent:
284-
:start-after: start-tag-set
285-
:end-before: end-tag-set
286-
287-
Local Threshold
288-
```````````````
289-
290-
If multiple replica-set members match the read preference and tag sets you specify,
291-
the {+php-library+} reads from the nearest replica-set members, chosen according to
292-
their ping time.
293-
294-
By default, the library uses only members whose ping times are within 15 milliseconds
295-
of the nearest member for queries. To distribute reads between members with
296-
higher latencies, pass an options array to the ``MongoDB\Client`` constructor that
297-
sets the ``localThresholdMS`` option.
298-
299-
The following example specifies a local threshold of 35 milliseconds:
300-
301-
.. literalinclude:: /includes/databases-collections/databases-collections.php
302-
:language: php
303-
:dedent:
304-
:start-after: start-local-threshold
305-
:end-before: end-local-threshold
306-
307-
In the preceding example, the {+php-library+} distributes reads among matching members
308-
within 35 milliseconds of the closest member's ping time.
309-
310-
.. note::
311-
312-
The {+php-library+} ignores the value of ``localThresholdMS`` when communicating with a
313-
replica set through a ``mongos`` instance. In this case, use the
314-
:manual:`localThreshold </reference/program/mongos/#std-option-mongos.--localThreshold>`
315-
command-line option.
206+
To learn more about setting a read preference, read concern, and write concern,
207+
see the :ref:`php-read-write-pref` guide.
316208

317209
API Documentation
318210
-----------------

source/includes/read-write-pref.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
require 'vendor/autoload.php';
3+
4+
use MongoDB\Driver\ReadConcern;
5+
use MongoDB\Driver\ReadPreference;
6+
use MongoDB\Driver\WriteConcern;
7+
use MongoDB\Client;
8+
9+
// start-client-settings
10+
$clientOptions = [
11+
'readPreference' => 'secondary',
12+
'readConcernLevel' => 'local',
13+
'w' => '2',
14+
];
15+
16+
$client = new Client('mongodb://localhost:27017', $clientOptions);
17+
// end-client-settings
18+
19+
// start-client-settings-uri
20+
$uri = 'mongodb://localhost:27017/?readPreference=secondary&readConcernLevel=local&w=2';
21+
$client = new Client($uri);
22+
// end-client-settings-uri
23+
24+
// start-session-settings
25+
$sessionOptions = [
26+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY_PREFERRED),
27+
'readConcern' => new ReadConcern(ReadConcern::LOCAL),
28+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
29+
];
30+
31+
$session = $client->startSession($sessionOptions);
32+
// end-session-settings
33+
34+
// start-transaction-settings
35+
$transactionOptions = [
36+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY),
37+
'readConcern' => new ReadConcern(ReadConcern::MAJORITY),
38+
'writeConcern' => new WriteConcern(1),
39+
];
40+
41+
$session->startTransaction($transactionOptions);
42+
// end-transaction-settings
43+
44+
// Sets read and write settings for the "test_database" database
45+
// start-database-settings
46+
$db = $client->selectDatabase('test_database', [
47+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY_PREFERRED),
48+
'readConcern' => new ReadConcern(ReadConcern::AVAILABLE),
49+
'writeConcern' => new WriteConcern(WriteConcern::MAJORITY),
50+
]);
51+
// end-database-settings
52+
53+
// Sets read and write settings for the "test_collection" collection
54+
// start-collection-settings
55+
$collection = $client->selectCollection('test_database', 'test_collection', [
56+
'readPreference' => new ReadPreference(ReadPreference::SECONDARY_PREFERRED),
57+
'readConcern' => new ReadConcern(ReadConcern::AVAILABLE),
58+
'writeConcern' => new WriteConcern(0),
59+
]);
60+
61+
// end-collection-settings
62+
63+
// Instructs the library to prefer reads from secondary replica set members
64+
// located in New York, followed by a secondary in San Francisco, and
65+
// lastly fall back to any secondary.
66+
// start-tag-set
67+
$readPreference = new ReadPreference(
68+
ReadPreference::RP_SECONDARY,
69+
[
70+
['dc' => 'ny'],
71+
['dc' => 'sf'],
72+
[],
73+
],
74+
);
75+
76+
$db = $client->selectDatabase(
77+
'test_database',
78+
['readPreference' => $readPreference],
79+
);
80+
81+
// end-tag-set
82+
83+
// Instructs the library to distribute reads between members within 35 milliseconds
84+
// of the closest member's ping time
85+
// start-local-threshold
86+
$options = [
87+
'replicaSet' => 'repl0',
88+
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
89+
'localThresholdMS' => 35,
90+
];
91+
92+
$client = new Client('mongodb://localhost:27017', [], $options);
93+
// end-local-threshold

source/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ MongoDB PHP Library
1818
/indexes
1919
/monitoring
2020
/security
21+
/read-write-pref
2122
/tutorial
2223
/upgrade
2324
/reference

0 commit comments

Comments
 (0)