Skip to content

Commit 3d7ae37

Browse files
committed
DOCSP-41965: Read and write settings
1 parent 9ad7615 commit 3d7ae37

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

source/databases-collections.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ You can change these settings by passing an options array to the
199199
``MongoDB\Client::selectDatabase()``, ``MongoDB\Client::selectCollection()``, or
200200
``MongoDB\Database::selectCollection()`` methods.
201201

202+
To learn more about setting a read preference, read concern, and write concern,
203+
see the :ref:`php-crud-write-read-pref` guide.
204+
202205
To change the read preference, read concern, and write concern of your database
203206
or collection, set the following options in the array parameter:
204207

source/read-write-pref.txt

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
.. _php-read-write-pref:
2+
3+
===============================================
4+
Specify How CRUD Operations Run on Replica Sets
5+
===============================================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: customize, preferences, replica set, consistency
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to use the **write concern**, **read concern**, and
24+
**read preference** configurations to modify the way that MongoDB runs
25+
create, read, update, and delete (CRUD) operations on replica sets.
26+
27+
You can set write concern, read concern, and read preference options at the following
28+
levels:
29+
30+
- Client, which sets the *default for all operation executions* unless overridden
31+
- Session
32+
- Transaction
33+
- Database
34+
- Collection
35+
36+
This list also indicates the increasing order of precedence of the option settings. For
37+
example, if you set a read concern level for a transaction, it will override a read
38+
concern level set for the client.
39+
40+
These options allow you to customize the causal consistency and availability of the data
41+
in your replica sets.
42+
43+
.. _php-read-write-config:
44+
45+
Configure Read and Write Operations
46+
-----------------------------------
47+
48+
This section shows how to configure the read preference, read concern, and write
49+
concern at various levels by passing an options array parameter to the following
50+
objects and methods:
51+
52+
- :ref:`MongoDB\\Client <php-read-write-client>`
53+
- :ref:`MongoDB\\Driver\\Session <php-read-write-session>`
54+
- :ref:`MongoDB\\with_transaction() <php-read-write-transaction>`
55+
- :ref:`MongoDB\\Database <php-read-write-database>`
56+
- :ref:`MongoDB\\Collection <php-read-write-collection>`
57+
58+
In the options parameter, specify the following values:
59+
60+
- ``readPreference``: Sets the read preference. For a list of available read
61+
preferences, see :php:`MongoDB\Driver\ReadPreference <mongodb-driver-readpreference>`
62+
in the extension API documentation.
63+
- ``readConcern``: Sets the read concern. For a list of available read
64+
concerns, see :php:`MongoDB\Driver\ReadConcern <mongodb-driver-readconcern>`
65+
in the extension API documentation.
66+
- ``writeConcern``: Sets the write concern. For a list of available write
67+
concerns, see :php:`MongoDB\Driver\WriteConcern <mongodb-driver-writeconcern>`
68+
in the extension API documentation.
69+
70+
.. _php-read-write-client:
71+
72+
Client Configuration Example
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
75+
The following example shows how to set the read preference, read concern, and
76+
write concern of a ``MongoDB\Client`` instance by passing an array to
77+
the constructor:
78+
79+
.. literalinclude:: /includes/databases-collections/databases-collections.php
80+
:language: php
81+
:dedent:
82+
:start-after: start-database-settings
83+
:end-before: end-database-settings
84+
85+
.. _php-read-write-session:
86+
87+
Session Configuration Example
88+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89+
90+
.. _php-read-write-transaction:
91+
92+
Transaction Configuration Example
93+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94+
95+
.. _php-read-write-database:
96+
97+
Database Configuration Example
98+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99+
100+
The following example shows how to set the read preference, read concern, and
101+
write concern of a database called ``test_database`` by passing an options
102+
array to ``selectDatabase()``:
103+
104+
.. literalinclude:: /includes/databases-collections/databases-collections.php
105+
:language: php
106+
:dedent:
107+
:start-after: start-database-settings
108+
:end-before: end-database-settings
109+
110+
.. _php-read-write-collection:
111+
112+
Collection Configuration Example
113+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114+
115+
The following example shows how to set the read preference, read concern, and
116+
write concern of a collection called ``test_collection`` by passing an options
117+
array to ``selectCollection()``:
118+
119+
.. literalinclude:: /includes/databases-collections/databases-collections.php
120+
:language: php
121+
:dedent:
122+
:start-after: start-collection-settings
123+
:end-before: end-collection-settings
124+
125+
Advanced Read Configurations
126+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127+
128+
Tag Sets
129+
````````
130+
131+
In {+mdb-server+}, you can apply key-value :manual:`tags
132+
</core/read-preference-tags/>` to replica-set
133+
members according to any criteria you choose. You can then use
134+
those tags to target one or more members for a read operation.
135+
136+
By default, the {+php-library+} ignores tags when choosing a member
137+
to read from. To instruct the {+php-library+} to prefer certain tags,
138+
pass them as a parameter to your ``MongoDB\Driver\ReadPreference`` class
139+
constructor. Then, set the ``MongoDB\Driver\ReadPreference`` object as
140+
the value of the ``readPreference`` database option.
141+
142+
This code example sets the ``readPreference`` option to a tag set
143+
that instructs ``test_database`` to prefer reads from secondary replica set
144+
members in the following order:
145+
146+
1. Members from the New York data center (``['dc' => 'ny']``)
147+
#. Members from the San Francisco data center (``['dc' => 'sf']``)
148+
#. Any secondary members (``[]``)
149+
150+
.. literalinclude:: /includes/databases-collections/databases-collections.php
151+
:language: php
152+
:dedent:
153+
:start-after: start-tag-set
154+
:end-before: end-tag-set
155+
156+
Local Threshold
157+
```````````````
158+
159+
If multiple replica-set members match the read preference and tag sets you specify,
160+
the {+php-library+} reads from the nearest replica-set members, chosen according to
161+
their ping time.
162+
163+
By default, the library uses only members whose ping times are within 15 milliseconds
164+
of the nearest member for queries. To distribute reads between members with
165+
higher latencies, pass an options array to the ``MongoDB\Client`` constructor that
166+
sets the ``localThresholdMS`` option.
167+
168+
The following example specifies a local threshold of 35 milliseconds:
169+
170+
.. literalinclude:: /includes/databases-collections/databases-collections.php
171+
:language: php
172+
:dedent:
173+
:start-after: start-local-threshold
174+
:end-before: end-local-threshold
175+
176+
In the preceding example, the {+php-library+} distributes reads among matching members
177+
within 35 milliseconds of the closest member's ping time.
178+
179+
.. note::
180+
181+
The {+php-library+} ignores the value of ``localThresholdMS`` when communicating with a
182+
replica set through a ``mongos`` instance. In this case, use the
183+
:manual:`localThreshold </reference/program/mongos/#std-option-mongos.--localThreshold>`
184+
command-line option.
185+
186+
187+
Additional Information
188+
----------------------
189+
190+
To learn more about read preferences and read and write concerns, see the
191+
following guides in the {+mdb-server+} manual:
192+
193+
- :manual:`Read Preference </core/read-preference/>`
194+
- :manual:`Read Concern </reference/read-concern/>`
195+
- :manual:`Write Concern </reference/write-concern/>`
196+
197+
API Documentation
198+
~~~~~~~~~~~~~~~~~
199+
200+
To learn more about any of the methods or types discussed in this
201+
guide, see the following API documentation:

0 commit comments

Comments
 (0)