@@ -53,8 +53,6 @@ Using a MongoDB\\Client Object
53
53
54
54
You can pass connection options to the ``MongoDB\Client`` constructor
55
55
instead of including them in your connection URI.
56
- Configuring the connection this way makes it easier to
57
- change settings at runtime and helps you catch errors during compilation.
58
56
59
57
The following example shows how to use the ``$uriOptions`` parameter of the
60
58
``MongoDB\Client`` constructor to set connection options:
@@ -68,15 +66,30 @@ The following example shows how to use the ``$uriOptions`` parameter of the
68
66
69
67
.. note::
70
68
71
- If you specify an option in both the ``MongoDB\Client `` object and in the connection
72
- URI, the value in the ``MongoDB\Client`` object takes precedence.
69
+ If you specify an option in both the ``$uriOptions `` parameter and in the connection
70
+ URI, the value in ``$uriOptions`` takes precedence.
73
71
74
72
Connection URI Options
75
73
----------------------
76
74
77
75
The following sections describe the options that you can set for your connection to
78
76
MongoDB. Each connection option links to its corresponding
79
- entry in the {+mdb-server+} manual.
77
+ entry in the {+mdb-server+} manual.
78
+
79
+ .. important:: Percent-Encoding
80
+
81
+ If the value of a connection option contains special characters, you must
82
+ :wikipedia:`percent-encode <Percent-encoding>` the value before including it
83
+ in the connection URI. You can use the ``rawurlencode()`` method to encode
84
+ these values according to the URI syntax specified in RFC 3986.
85
+
86
+ Don't percent-encode connection options when including them in the
87
+ ``$uriOptions`` parameter.
88
+
89
+ To learn more, see the following resources:
90
+
91
+ - `RFC 3986 <http://www.faqs.org/rfcs/rfc3986.html>`__
92
+ - `rawurlencode <{+php-manual+}/rawurlencode>`__ in the PHP manual
80
93
81
94
Replica Set Options
82
95
~~~~~~~~~~~~~~~~~~~
@@ -88,14 +101,14 @@ Replica Set Options
88
101
* - Connection Option
89
102
- Description
90
103
91
- * - :manual:`directConnection </reference/connection-string/#mongodb-urioption-urioption.directConnection>`
104
+ * - :manual:`directConnection </reference/connection-string-options /#mongodb-urioption-urioption.directConnection>`
92
105
- | **Data Type**: {+bool-data-type+}
93
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'directConnection' => true ];``
106
+ | **MongoDB\\Client Example**: ``$uriOptions = ['directConnection' => true];``
94
107
| **Connection URI Example**: ``directConnection=true``
95
108
96
- * - :manual:`replicaSet </reference/connection-string/#mongodb-urioption-urioption.replicaSet>`
109
+ * - :manual:`replicaSet </reference/connection-string-options /#mongodb-urioption-urioption.replicaSet>`
97
110
- | **Data Type**: {+string-data-type+}
98
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'replicaSet' => 'replicaSetName' ];``
111
+ | **MongoDB\\Client Example**: ``$uriOptions = ['replicaSet' => 'replicaSetName'];``
99
112
| **Connection URI Example**: ``replicaSet=replicaSetName``
100
113
101
114
Connection Options
@@ -116,14 +129,14 @@ Timeout Options
116
129
* - Connection Option
117
130
- Description
118
131
119
- * - :manual:`connectTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.connecttimeoutms>`
132
+ * - :manual:`connectTimeoutMS </reference/connection-string-options /#mongodb-urioption-urioption.connecttimeoutms>`
120
133
- | **Data Type**: {+int-data-type+}
121
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'connectTimeoutMS' => 20000 ];``
134
+ | **MongoDB\\Client Example**: ``$uriOptions = ['connectTimeoutMS' => 20000];``
122
135
| **Connection URI Example**: ``connectTimeoutMS=20000``
123
136
124
- * - :manual:`socketTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.sockettimeoutms>`
137
+ * - :manual:`socketTimeoutMS </reference/connection-string-options /#mongodb-urioption-urioption.sockettimeoutms>`
125
138
- | **Data Type**: {+int-data-type+}
126
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'socketTimeoutMS' => 400000 ];``
139
+ | **MongoDB\\Client Example**: ``$uriOptions = ['socketTimeoutMS' => 400000];``
127
140
| **Connection URI Example**: ``socketTimeoutMS=400000``
128
141
129
142
.. _php-compression-options:
@@ -138,36 +151,16 @@ Compression Options
138
151
* - Connection Option
139
152
- Description
140
153
141
- * - :manual:`compressors </reference/connection-string/#mongodb-urioption-urioption.compressors>`
154
+ * - :manual:`compressors </reference/connection-string-options /#mongodb-urioption-urioption.compressors>`
142
155
- | **Data Type**: {+string-data-type+}
143
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'compressors' => 'snappy,zstd,zlib' ];``
156
+ | **MongoDB\\Client Example**: ``$uriOptions = ['compressors' => 'snappy,zstd,zlib'];``
144
157
| **Connection URI Example**: ``compressors=snappy,zstd,zlib``
145
158
146
- * - :manual:`zlibCompressionLevel </reference/connection-string/#mongodb-urioption-urioption.zlibcompressionlevel>`
159
+ * - :manual:`zlibCompressionLevel </reference/connection-string-options /#mongodb-urioption-urioption.zlibcompressionlevel>`
147
160
- | **Data Type**: {+int-data-type+}
148
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'zlibCompressionLevel' => 3 ];``
161
+ | **MongoDB\\Client Example**: ``$uriOptions = ['zlibCompressionLevel' => 3];``
149
162
| **Connection URI Example**: ``zlibCompressionLevel=3``
150
163
151
- Connection Pool Options
152
- ~~~~~~~~~~~~~~~~~~~~~~~
153
-
154
- .. list-table::
155
- :header-rows: 1
156
- :stub-columns: 1
157
-
158
- * - Connection Option
159
- - Description
160
-
161
- * - :manual:`maxPoolSize </reference/connection-string/#mongodb-urioption-urioption.maxpoolsize>`
162
- - | **Data Type**: {+int-data-type+}
163
- | **MongoDB\\Client Example**: N/A
164
- | **Connection URI Example**: ``maxPoolSize=75``
165
-
166
- * - :manual:`waitQueueTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.waitqueuetimeoutms>`
167
- - | **Data Type**: {+int-data-type+}
168
- | **MongoDB\\Client Example**: N/A
169
- | **Connection URI Example**: ``waitQueueTimeoutMS=10000``
170
-
171
164
Write Concern Options
172
165
~~~~~~~~~~~~~~~~~~~~~
173
166
@@ -178,19 +171,19 @@ Write Concern Options
178
171
* - Connection Option
179
172
- Description
180
173
181
- * - :manual:`w </reference/connection-string/#mongodb-urioption-urioption.w>`
174
+ * - :manual:`w </reference/connection-string-options /#mongodb-urioption-urioption.w>`
182
175
- | **Data Type**: {+string-data-type+}
183
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'w' => 'majority' ];``
176
+ | **MongoDB\\Client Example**: ``$uriOptions = ['w' => 'majority'];``
184
177
| **Connection URI Example**: ``w=majority``
185
178
186
- * - :manual:`wTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.wtimeoutms>`
179
+ * - :manual:`wTimeoutMS </reference/connection-string-options /#mongodb-urioption-urioption.wtimeoutms>`
187
180
- | **Data Type**: {+int-data-type+}
188
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'wTimeoutMS' => 10000 ];``
181
+ | **MongoDB\\Client Example**: ``$uriOptions = ['wTimeoutMS' => 10000];``
189
182
| **Connection URI Example**: ``wTimeoutMS=10000``
190
183
191
- * - :manual:`journal </reference/connection-string/#mongodb-urioption-urioption.journal>`
184
+ * - :manual:`journal </reference/connection-string-options /#mongodb-urioption-urioption.journal>`
192
185
- | **Data Type**: {+bool-data-type+}
193
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'journal' => true ];``
186
+ | **MongoDB\\Client Example**: ``$uriOptions = ['journal' => true];``
194
187
| **Connection URI Example**: ``journal=true``
195
188
196
189
Read Concern Options
@@ -203,9 +196,9 @@ Read Concern Options
203
196
* - Connection Option
204
197
- Description
205
198
206
- * - :manual:`readConcernLevel </reference/connection-string/#mongodb-urioption-urioption.readconcernlevel>`
199
+ * - :manual:`readConcernLevel </reference/connection-string-options /#mongodb-urioption-urioption.readconcernlevel>`
207
200
- | **Data Type**: {+string-data-type+}
208
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'readConcernLevel' => 'majority' ];``
201
+ | **MongoDB\\Client Example**: ``$uriOptions = ['readConcernLevel' => 'majority'];``
209
202
| **Connection URI Example**: ``readConcernLevel=majority``
210
203
211
204
Read Preference Options
@@ -219,57 +212,36 @@ Read Preference Options
219
212
* - Connection Option
220
213
- Description
221
214
222
- * - :manual:`readPreference </reference/connection-string/#mongodb-urioption-urioption.readpreference>`
215
+ * - :manual:`readPreference </reference/connection-string-options /#mongodb-urioption-urioption.readpreference>`
223
216
- | **Data Type**: `MongoDB\\Driver\\ReadPreference <https://www.php.net/manual/en/class.mongodb-driver-readpreference.php>`__
224
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'readPreference' => 'secondaryPreferred' ];``
217
+ | **MongoDB\\Client Example**: ``$uriOptions = ['readPreference' => 'secondaryPreferred'];``
225
218
| **Connection URI Example**: ``readPreference=secondaryPreferred``
226
219
227
- * - :manual:`maxStalenessSeconds </reference/connection-string/#mongodb-urioption-urioption.maxstalenessseconds>`
220
+ * - :manual:`maxStalenessSeconds </reference/connection-string-options /#mongodb-urioption-urioption.maxstalenessseconds>`
228
221
- | **Data Type**: {+int-data-type+}
229
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'maxStalenessSeconds' => 30 ];``
222
+ | **MongoDB\\Client Example**: ``$uriOptions = ['maxStalenessSeconds' => 30];``
230
223
| **Connection URI Example**: ``maxStalenessSeconds=30``
231
224
232
- * - :manual:`readPreferenceTags </reference/connection-string/#mongodb-urioption-urioption.readpreferencetags>`
225
+ * - :manual:`readPreferenceTags </reference/connection-string-options /#mongodb-urioption-urioption.readpreferencetags>`
233
226
- | **Data Type**: ``array``
234
227
| **MongoDB\\Client Example**:
235
228
236
229
.. code-block:: php
237
230
238
231
$uriOptions = [
239
- 'readPreferenceTags' => [
240
- [ 'region ' => 'east' ],
241
- [ 'region' => 'west']
242
- ]
243
- ];``
232
+ 'readPreferenceTags' => [
233
+ ['dc ' => 'ny', 'rack' => 'r1' ],
234
+ [],
235
+ ],
236
+ ];
244
237
245
- **Connection URI Example**: ``readPreferenceTags=region:east,region:west ``
238
+ **Connection URI Example**: ``readPreferenceTags=dc:ny,rack:r1&readPreferenceTags= ``
246
239
247
240
Authentication Options
248
241
~~~~~~~~~~~~~~~~~~~~~~
249
242
250
- .. list-table::
251
- :header-rows: 1
252
- :stub-columns: 1
253
-
254
- * - Connection Option
255
- - Description
256
-
257
- * - :manual:`authMechanism </reference/connection-string/#mongodb-urioption-urioption.authmechanism>`
258
- - | **Data Type**: {+string-data-type+}
259
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'authMechanism' => 'MONGODB-X509' ];``
260
- | **Connection URI Example**: ``authMechanism=MONGODB-X509``
261
-
262
- * - :manual:`authMechanismProperties </reference/connection-string/#mongodb-urioption-urioption.authmechanismproperties>`
263
- - | **Data Type**: {+string-data-type+}
264
- | **MongoDB\\Client Example**:
265
- | ``$uriOptions = [ 'authMechanismProperties' => 'AWS_SESSION_TOKEN:12345' ];``
266
- |
267
- | **Connection URI Example**: ``authMechanismProperties=AWS_SESSION_TOKEN:12345``
268
-
269
- * - :manual:`authSource </reference/connection-string/#mongodb-urioption-urioption.authsource>`
270
- - | **Data Type**: {+string-data-type+}
271
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'authSource' => 'admin' ];``
272
- | **Connection URI Example**: ``authSource=admin``
243
+ To learn about the authentication options available in the {+driver-short+}, see
244
+ :ref:`Authentication Mechanisms. <php-auth>`
273
245
274
246
Server Selection and Discovery Options
275
247
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -282,29 +254,29 @@ Server Selection and Discovery Options
282
254
* - Connection Option
283
255
- Description
284
256
285
- * - :manual:`localThresholdMS </reference/connection-string/#mongodb-urioption-urioption.localthresholdms>`
257
+ * - :manual:`localThresholdMS </reference/connection-string-options /#mongodb-urioption-urioption.localthresholdms>`
286
258
- | **Data Type**: {+int-data-type+}
287
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'localThresholdMS' => 20 ];``
259
+ | **MongoDB\\Client Example**: ``$uriOptions = ['localThresholdMS' => 20];``
288
260
| **Connection URI Example**: ``localThresholdMS=20``
289
261
290
- * - :manual:`serverSelectionTimeoutMS </reference/connection-string/#mongodb-urioption-urioption.serverselectiontimeoutms>`
262
+ * - :manual:`serverSelectionTimeoutMS </reference/connection-string-options /#mongodb-urioption-urioption.serverselectiontimeoutms>`
291
263
- | **Data Type**: {+int-data-type+}
292
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'serverSelectionTimeoutMS' => 40000 ];``
264
+ | **MongoDB\\Client Example**: ``$uriOptions = ['serverSelectionTimeoutMS' => 5000 ];``
293
265
| **Connection URI Example**: ``serverSelectionTimeoutMS=40000``
294
266
295
- * - :manual:`serverSelectionTryOnce </reference/connection-string/#mongodb-urioption-urioption.serverselectiontryonce>`
267
+ * - :manual:`serverSelectionTryOnce </reference/connection-string-options /#mongodb-urioption-urioption.serverselectiontryonce>`
296
268
- | **Data Type**: {+bool-data-type+}
297
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'serverSelectionTryOnce' => false ];``
269
+ | **MongoDB\\Client Example**: ``$uriOptions = ['serverSelectionTryOnce' => false];``
298
270
| **Connection URI Example**: ``serverSelectionTryOnce=false``
299
271
300
- * - :manual:`heartbeatFrequencyMS </reference/connection-string/#mongodb-urioption-urioption.heartbeatfrequencyms>`
272
+ * - :manual:`heartbeatFrequencyMS </reference/connection-string-options /#mongodb-urioption-urioption.heartbeatfrequencyms>`
301
273
- | **Data Type**: {+int-data-type+}
302
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'heartbeatFrequencyMS' => 50000 ];``
274
+ | **MongoDB\\Client Example**: ``$uriOptions = ['heartbeatFrequencyMS' => 50000];``
303
275
| **Connection URI Example**: ``heartbeatFrequencyMS=50000``
304
276
305
- * - :manual:`socketCheckIntervalMS </reference/connection-string/#mongodb-urioption-urioption.socketcheckintervalms>`
277
+ * - :manual:`socketCheckIntervalMS </reference/connection-string-options /#mongodb-urioption-urioption.socketcheckintervalms>`
306
278
- | **Data Type**: {+int-data-type+}
307
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'socketCheckIntervalMS' => 4000 ];``
279
+ | **MongoDB\\Client Example**: ``$uriOptions = ['socketCheckIntervalMS' => 4000];``
308
280
| **Connection URI Example**: ``socketCheckIntervalMS=4000``
309
281
310
282
Miscellaneous Configuration
@@ -317,29 +289,29 @@ Miscellaneous Configuration
317
289
* - Connection Option
318
290
- Description
319
291
320
- * - :manual:`appName </reference/connection-string/#mongodb-urioption-urioption.appname>`
292
+ * - :manual:`appName </reference/connection-string-options /#mongodb-urioption-urioption.appname>`
321
293
- | **Data Type**: {+string-data-type+}
322
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'appName' => 'myApp' ];``
294
+ | **MongoDB\\Client Example**: ``$uriOptions = ['appName' => 'myApp'];``
323
295
| **Connection URI Example**: ``appName=myApp``
324
296
325
- * - :manual:`retryReads </reference/connection-string/#mongodb-urioption-urioption.retryreads>`
297
+ * - :manual:`retryReads </reference/connection-string-options /#mongodb-urioption-urioption.retryreads>`
326
298
- | **Data Type**: {+bool-data-type+}
327
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'retryReads' => false ];``
299
+ | **MongoDB\\Client Example**: ``$uriOptions = ['retryReads' => false];``
328
300
| **Connection URI Example**: ``retryReads=false``
329
301
330
- * - :manual:`retryWrites </reference/connection-string/#mongodb-urioption-urioption.retrywrites>`
302
+ * - :manual:`retryWrites </reference/connection-string-options /#mongodb-urioption-urioption.retrywrites>`
331
303
- | **Data Type**: {+bool-data-type+}
332
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'retryWrites' => false ];``
304
+ | **MongoDB\\Client Example**: ``$uriOptions = ['retryWrites' => false];``
333
305
| **Connection URI Example**: ``retryWrites=false``
334
306
335
- * - :manual:`loadBalanced </reference/connection-string/#mongodb-urioption-urioption.loadbalanced>`
307
+ * - :manual:`loadBalanced </reference/connection-string-options /#mongodb-urioption-urioption.loadbalanced>`
336
308
- | **Data Type**: {+bool-data-type+}
337
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'loadBalanced' => true ];``
309
+ | **MongoDB\\Client Example**: ``$uriOptions = ['loadBalanced' => true];``
338
310
| **Connection URI Example**: ``loadBalanced=true``
339
311
340
- * - :manual:`srvMaxHosts </reference/connection-string/#mongodb-urioption-urioption.srvmaxhosts>`
312
+ * - :manual:`srvMaxHosts </reference/connection-string-options /#mongodb-urioption-urioption.srvmaxhosts>`
341
313
- | **Data Type**: {+int-data-type+}
342
- | **MongoDB\\Client Example**: ``$uriOptions = [ 'srvMaxHosts' => 5 ];``
314
+ | **MongoDB\\Client Example**: ``$uriOptions = ['srvMaxHosts' => 5];``
343
315
| **Connection URI Example**: ``srvMaxHosts=5``
344
316
345
317
API Documentation
0 commit comments