1
1
# MongoDB\Client
2
2
3
- ` MongoDB\Client ` serves as an entry point for the library and driver. It is
4
- constructed with the same arguments as the driver's ` MongoDB\Driver\Manager `
5
- class, which it composes. Additional reference may be found in the
6
- [ ` MongoDB\Driver\Manager::__construct ` ] ( http://php.net/manual/en/mongodb-driver-manager.construct.php] )
7
- and
8
- [ Connection String] ( https://docs.mongodb.org/manual/reference/connection-string/ )
9
- documentation.
3
+ The MongoDB\Client class serves as an entry point for the library. It is the
4
+ preferred class for connecting to a MongoDB server or cluster of servers and
5
+ serves as a gateway for accessing individual databases and collections. It is
6
+ analogous to the driver's [ MongoDB\Driver\Manager] [ manager ] class, which it
7
+ composes.
10
8
9
+ [ manager ] : http://php.net/mongodb-driver-manager
10
+
11
+ ---
12
+
13
+ ## __ construct()
14
+
15
+ ``` php
16
+ function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
11
17
```
12
- /* By default, the driver connects to mongodb://localhost:27017 */
13
- $client = new MongoDB\Client;
14
18
15
- /* Any URI options will be merged into the URI string */
19
+ Constructs a new Client instance.
20
+
21
+ Additional URI options may be provided as the second argument and will take
22
+ precedence over any like options present in the URI string (e.g. authentication
23
+ credentials, query string parameters).
24
+
25
+ Driver options may be provided as the third argument. In addition to any options
26
+ supported by the extension, this library allows you to specify a default
27
+ type map to apply to the cursors it creates. A more thorough description of type
28
+ maps may be found in the driver's [ Persistence documentation] [ typemap ] .
29
+
30
+ [ typemap ] : http://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps
31
+
32
+ ### Supported URI Options
33
+
34
+ See [ MongoDB\Driver\Manager::__ construct()] [ manager-construct ] and the
35
+ [ MongoDB manual] [ connection-string ] .
36
+
37
+ [ manager-construct ] : http://php.net/manual/en/mongodb-driver-manager.construct.php
38
+ [ connection-string ] : https://docs.mongodb.org/manual/reference/connection-string/
39
+
40
+ ### Supported Driver Options
41
+
42
+ typeMap (array)
43
+ : Default type map for cursors and BSON documents.
44
+
45
+ ### Example
46
+
47
+ By default, the driver connects to a standalone server on localhost via port
48
+ 27017 . The following example demonstrates how to connect to a replica set.
49
+ Additionally, it demonstrates a replica set with a custom read preference:
50
+
51
+ ```
52
+ <?php
53
+
16
54
$client = new MongoDB\Client(
17
55
'mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet',
18
- ['readPreference' => 'secondaryPreferred']
56
+ [
57
+ 'readPreference' => 'secondaryPreferred'
58
+ ]
19
59
);
20
60
```
21
61
22
- Driver options may be provided as the third argument. In addition to options
23
- supported by the extension, the PHP library allows you to specify a default
24
- type map to apply to the cursors it creates. A more thorough description of type
25
- maps may be found in the driver's
26
- [ Persistence documentation] ( http://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps ) .
62
+ By default, the library will unserialize BSON documents and arrays as
63
+ MongoDB\Model\BSONDocument and MongoDB\Model\BSONArray objects, respectively.
64
+ The following example demonstrates how to have the library unserialize
65
+ everything as a PHP array, as was done in the legacy
66
+ [ mongo extension] [ ext-mongo ] :
67
+
68
+ [ ext-mongo ] : http://php.net/mongo
27
69
28
70
```
29
- /* This example instructs the library to unserialize root and embedded BSON
30
- * documents as PHP arrays, like the legacy driver (i.e. ext-mongo). */
31
- $client = new MongoDB\Client(null, [], [
32
- 'typeMap' => ['root' => 'array', 'document' => 'array'],
71
+ <?php
72
+
73
+ $client = new MongoDB\Client(
74
+ null,
75
+ [],
76
+ ['typeMap' => ['root' => 'array', 'document' => 'array', 'array' => 'array']]
33
77
);
34
78
```
35
79
36
- By default, the library will unserialize BSON documents and arrays as
37
- ` MongoDB\Model\BSONDocument ` and ` MongoDB\Model\BSONArray ` objects,
38
- respectively. Each of these model classes extends PHP's
39
- [ ` ArrayObject ` ] ( http://php.net/arrayobject ) class and implements the driver's
40
- [ ` MongoDB\BSON\Serializable ` ] ( http://php.net/mongodb-bson-serializable ) and
41
- [ ` MongoDB\BSON\Unserializable ` ] ( http://php.net/mongodb-bson-unserializable )
42
- interfaces.
80
+ ### See Also
81
+
82
+ * [ MongoDB\Driver\Manager::__ construct()] [ manager-construct ]
83
+ * [ MongoDB Manual: Connection String] [ connection-string ]
43
84
44
- ## Selecting Databases and Collections
85
+ ---
45
86
46
- The Client class provides methods for creating Database or Collection instances
47
- (using its internal Manager instance). When selecting a Database or Collection,
48
- the child will inherit options (e.g. read preference, type map) from the Client.
49
- New options may also be provided to the ` selectDatabase() ` and
50
- ` selectCollection() ` methods.
87
+ ## __ get()
51
88
89
+ ``` php
90
+ function __get($databaseName): MongoDB\Database
52
91
```
92
+
93
+ Select a database.
94
+
95
+ The Database will inherit options (e.g. read preference, type map) from the
96
+ Client object. Use [ selectDatabase()] ( #selectdatabase ) to override any options.
97
+
98
+ ** Note:** databases whose names contain special characters (e.g. "-") may be
99
+ selected with complex syntax (e.g. ` $client->{"that-database"} ` ) or
100
+ [ selectDatabase()] ( #selectdatabase ) .
101
+
102
+ ### Example
103
+
104
+ The following example selects the "demo" and "another-app" databases:
105
+
106
+ ```
107
+ <?php
108
+
53
109
$client = new MongoDB\Client;
54
110
55
- /* Select the "demo" database */
56
- $db = $client->selectDatabase('demo');
111
+ $demo = $client->demo;
112
+ $anotherApp = $client->{'another-app'};
113
+ ```
57
114
58
- /* Select the "demo.users" collection */
59
- $collection = $client->selectCollection('demo', 'users');
115
+ ### See Also
116
+
117
+ * [ MongoDB\Client::selectDatabase()] ( #selectdatabase )
118
+ * [ PHP Manual: Property Overloading] ( http://php.net/oop5.overloading#object.get )
119
+
120
+ ---
60
121
61
- /* selectDatabase() and selectCollection() also take an options array, which can
62
- * override any options inherited from the Client. */
63
- $db = $client->selectDatabase('demo', [
64
- 'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
65
- ]);
122
+ ## dropDatabase
66
123
67
- /* The property accessor may also be used to select a database without
68
- * specifying additional options. PHP's complex syntax may be used for selecting
69
- * databases whose names contain special characters (e.g. "-"). */
70
- $db = $client->demo;
71
- $db = $client->{'another-app'};
124
+ ``` php
125
+ function dropDatabase($databaseName, array $options = []): array|object
72
126
```
73
127
74
- ## Database Management
128
+ Drop a database. Returns the command result document.
75
129
76
- The Client class has several methods for managing databases.
130
+ ### Supported Options
77
131
78
- ### Dropping Databases
132
+ typeMap (array)
133
+ : Type map for BSON deserialization. This will only be used for the returned
134
+ command result document.
135
+
136
+ ### Example
137
+
138
+ The following example drops the "demo" database:
79
139
80
140
```
141
+ <?php
142
+
81
143
$client = new MongoDB\Client;
82
144
83
145
$result = $client->dropDatabase('demo');
146
+
84
147
var_dump($result);
85
148
```
86
149
@@ -98,12 +161,36 @@ object(MongoDB\Model\BSONDocument)#8 (1) {
98
161
}
99
162
```
100
163
101
- ### Enumerating Databases
164
+ ### See Also
165
+
166
+ * [ MongoDB\Database::drop()] ( database.md#drop )
167
+ * [ MongoDB Manual: dropDatabase command] ( http://docs.mongodb.org/manual/reference/command/dropDatabase/ )
168
+
169
+ ---
170
+
171
+ ## listDatabases()
102
172
173
+ ``` php
174
+ function listDatabases(array $options = []): MongoDB\Model\DatabaseInfoIterator
103
175
```
176
+
177
+ Returns information for all database on the server. Elements in the returned
178
+ iterator will be MongoDB\Model\DatabaseInfo objects.
179
+
180
+ ### Supported Options
181
+
182
+ maxTimeMS (integer)
183
+ : The maximum amount of time to allow the query to run.
184
+
185
+ ### Example
186
+
187
+ The following example lists all databases on the server:
188
+
189
+ ```
190
+ <?php
191
+
104
192
$client = new MongoDB\Client;
105
193
106
- /* listDatabases() returns an iterator of MongoDB\Model\DatabaseInfo objects */
107
194
foreach ($client->listDatabases() as $databaseInfo) {
108
195
var_dump($databaseInfo);
109
196
}
@@ -129,3 +216,139 @@ object(MongoDB\Model\DatabaseInfo)#7 (3) {
129
216
bool(false)
130
217
}
131
218
```
219
+
220
+ ### See Also
221
+
222
+ * [ MongoDB Manual: listDatabases command] ( http://docs.mongodb.org/manual/reference/command/listDatabases/ )
223
+
224
+ ---
225
+
226
+ ## selectCollection()
227
+
228
+ ``` php
229
+ function selectCollection($databaseName, $collectionName, array $options = []): MongoDB\Collection
230
+ ```
231
+
232
+ Select a collection on the server.
233
+
234
+ The Collection will inherit options (e.g. read preference, type map) from the
235
+ Client object unless otherwise specified.
236
+
237
+ ### Supported Options
238
+
239
+ readConcern (MongoDB\Driver\ReadConcern)
240
+ : The default read concern to use for collection operations. Defaults to the
241
+ Client's read concern.
242
+
243
+ readPreference (MongoDB\Driver\ReadPreference)
244
+ : The default read preference to use for collection operations. Defaults to
245
+ the Client's read preference.
246
+
247
+ typeMap (array)
248
+ : Default type map for cursors and BSON documents. Defaults to the Client's
249
+ type map.
250
+
251
+ writeConcern (MongoDB\Driver\WriteConcern)
252
+ : The default write concern to use for collection operations. Defaults to the
253
+ Client's write concern.
254
+
255
+ ### Example
256
+
257
+ The following example selects the "demo.users" collection:
258
+
259
+ ```
260
+ <?php
261
+
262
+ $client = new MongoDB\Client;
263
+
264
+ $collection = $client->selectCollection('demo', 'users');
265
+ ```
266
+
267
+ The following examples selects the "demo.users" collection with a custom read
268
+ preference:
269
+
270
+ ```
271
+ <?php
272
+
273
+ $client = new MongoDB\Client;
274
+
275
+ $collection = $client->selectCollection(
276
+ 'demo',
277
+ 'users',
278
+ [
279
+ 'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
280
+ ]
281
+ );
282
+ ```
283
+
284
+ ### See Also
285
+
286
+ * [ MongoDB\Collection::__ construct()] ( collection.md#__construct )
287
+ * [ MongoDB\Client::__ get()] ( #__get )
288
+
289
+
290
+ ---
291
+
292
+ ## selectDatabase()
293
+
294
+ ``` php
295
+ function selectDatabase($databaseName array $options = []): MongoDB\Collection
296
+ ```
297
+
298
+ Select a database on the server.
299
+
300
+ The Database will inherit options (e.g. read preference, type map) from the
301
+ Client object unless otherwise specified.
302
+
303
+ ### Supported Options
304
+
305
+ readConcern (MongoDB\Driver\ReadConcern)
306
+ : The default read concern to use for database operations. Defaults to the
307
+ Client's read concern.
308
+
309
+ readPreference (MongoDB\Driver\ReadPreference)
310
+ : The default read preference to use for database operations. Defaults to the
311
+ Client's read preference.
312
+
313
+ typeMap (array)
314
+ : Default type map for cursors and BSON documents. Defaults to the Client's
315
+ type map.
316
+
317
+ writeConcern (MongoDB\Driver\WriteConcern)
318
+ : The default write concern to use for database operations. Defaults to the
319
+ Client's write concern.
320
+
321
+ ### Example
322
+
323
+ The following example selects the "demo" database:
324
+
325
+ ```
326
+ <?php
327
+
328
+ $client = new MongoDB\Client;
329
+
330
+ $db = $client->selectDatabase('demo');
331
+ ```
332
+
333
+ The following examples selects the "demo" database with a custom read
334
+ preference:
335
+
336
+ ```
337
+ <?php
338
+
339
+ $client = new MongoDB\Client;
340
+
341
+ $db = $client->selectDatabase(
342
+ 'demo',
343
+ [
344
+ 'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
345
+ ]
346
+ );
347
+ ```
348
+
349
+ ### See Also
350
+
351
+ * [ MongoDB\Database::__ construct()] ( database.md#__construct )
352
+ * [ MongoDB\Client::__ get()] ( #__get )
353
+
354
+ ---
0 commit comments