Skip to content

Commit 6ff3578

Browse files
committed
JM feedback
1 parent c814318 commit 6ff3578

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

source/databases-collections.txt

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ The following example accesses a database named ``test_database``:
4545
:start-after: start-access-database
4646
:end-before: end-access-database
4747

48+
Alternatively, you can implicitly call the ``MongoDB\Client::__get()`` magic method on
49+
a client object. This method allows you to select a database by using the syntax for
50+
accessing a class property. The following example uses this shorthand syntax to access
51+
the ``test_database`` database:
52+
53+
.. literalinclude:: /includes/databases-collections/databases-collections.php
54+
:language: php
55+
:dedent:
56+
:start-after: start-access-database-short
57+
:end-before: end-access-database-short
58+
59+
.. tip::
60+
61+
To learn more about ``__get()`` and PHP magic methods, see the following resources:
62+
63+
- :phpmethod:`MongoDB\Client::__get()` in the API documentation
64+
- `Magic Methods <{+php-manual+}/language.oop5.magic.php>`__ in the PHP manual
65+
4866
.. _php-db-coll-access-collection:
4967

5068
Access a Collection
@@ -71,6 +89,20 @@ The following example accesses a collection named ``test_collection`` by using t
7189
MongoDB implicitly creates the collection when you first insert data
7290
into it.
7391

92+
Alternatively, you can implicitly call the ``MongoDB\Database::__get()`` magic method on
93+
a database object. This method allows you to select a collection by using the syntax for
94+
accessing a class property. The following example uses this shorthand syntax to access
95+
the ``test_collection`` collection:
96+
97+
.. literalinclude:: /includes/databases-collections/databases-collections.php
98+
:language: php
99+
:dedent:
100+
:start-after: start-access-collection-short
101+
:end-before: end-access-collection-short
102+
103+
To learn more, see the :phpmethod:`MongoDB\Database::__get()`
104+
API documentation.
105+
74106
.. _php-db-coll-create-collection:
75107

76108
Create a Collection
@@ -100,7 +132,7 @@ You can query for a list of collections in a database by calling the
100132
cursor containing all collections in the database and their associated metadata.
101133

102134
The following example calls the ``listCollections()`` method and iterates over
103-
the returned cursor to print the collections from the :ref:`php-db-coll-access-collection`
135+
the returned iterator to print the collections from the :ref:`php-db-coll-access-collection`
104136
and :ref:`php-db-coll-create-collection` examples:
105137

106138
.. io-code-block::
@@ -129,9 +161,6 @@ and :ref:`php-db-coll-create-collection` examples:
129161
...
130162
)
131163

132-
For more information about iterating over a cursor, see the :ref:`php-cursors`
133-
guide.
134-
135164
Delete a Collection
136165
-------------------
137166

@@ -164,22 +193,23 @@ read and write operations on a replica set by setting a **read concern** and a
164193
**write concern**.
165194

166195
By default, databases inherit read and write settings from the ``MongoDB\Client``
167-
instance, and collections inherit them from the database. You can change
168-
these settings on your database by passing an options array to the
169-
``MongoDB\Client::selectDatabase()``. You can change these settings on your collection
170-
by passing an options array to the ``MongoDB\Client::selectCollection()`` method.
196+
instance. Collections inherit these settings from the ``MongoDB\Client`` or
197+
``MongoDB\Database`` instance on which the ``selectCollection()`` method is called.
198+
You can change these settings by passing an options array to the
199+
``MongoDB\Client::selectDatabase()``, ``MongoDB\Client::selectCollection()``, or
200+
``MongoDB\Database::selectCollection()`` methods.
171201

172202
To change the read preference, read concern, and write concern of your database
173203
or collection, set the following options in the array parameter:
174204

175205
- ``readPreference``: Sets the read preference. For a list of available read
176-
preferences, see `MongoDB\\Driver\\ReadPreference <{+php-manual+}/class.mongodb-driver-readpreference>`__
206+
preferences, see :php:`MongoDB\Driver\ReadPreference <mongodb-driver-readpreference>`
177207
in the extension API documentation.
178208
- ``readConcern``: Sets the read concern. For a list of available read
179-
concerns, see `MongoDB\\Driver\\ReadConcern <{+php-manual+}/class.mongodb-driver-readconcern>`__
209+
concerns, see :php:`MongoDB\Driver\ReadConcern <mongodb-driver-readconcern>`
180210
in the extension API documentation.
181211
- ``writeConcern``: Sets the write concern. For a list of available write
182-
concerns, see `MongoDB\\Driver\\WriteConcern <{+php-manual+}/class.mongodb-driver-writeconcern>`__
212+
concerns, see :php:`MongoDB\Driver\WriteConcern <mongodb-driver-writeconcern>`
183213
in the extension API documentation.
184214

185215
.. tip::
@@ -217,8 +247,11 @@ array to ``selectCollection()``:
217247
:start-after: start-collection-settings
218248
:end-before: end-collection-settings
219249

250+
Advanced Read Configurations
251+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
252+
220253
Tag Sets
221-
~~~~~~~~
254+
````````
222255

223256
In {+mdb-server+}, you can apply key-value :manual:`tags
224257
</core/read-preference-tags/>` to replica-set
@@ -243,7 +276,7 @@ center (``'dc' => 'sf'``):
243276
:end-before: end-tag-set
244277

245278
Local Threshold
246-
~~~~~~~~~~~~~~~
279+
```````````````
247280

248281
If multiple replica-set members match the read preference and tag sets you specify,
249282
the {+php-library+} reads from the nearest replica-set members, chosen according to

source/includes/databases-collections/databases-collections.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
<?php
22
require 'vendor/autoload.php';
33

4-
use MongoDB\BSON\Document;
54
use MongoDB\Driver\ReadConcern;
65
use MongoDB\Driver\ReadPreference;
76
use MongoDB\Driver\WriteConcern;
87

9-
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset');
8+
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your connection URI');
109
$client = new MongoDB\Client($uri);
1110

1211
// Accesses the "test_database" database
1312
// start-access-database
1413
$db = $client->selectDatabase('test_database');
1514
// end-access-database
1615

16+
// Invokes the __get() method to access the "test_database" database
17+
// start-access-database-short
18+
$db = $client->test_database;
19+
// end-access-database-short
20+
1721
// Accesses the "test_collection" collection
1822
// start-access-collection
1923
$collection = $client->test_database->selectCollection('test_collection');
2024
// end-access-collection
2125

26+
// Invokes the __get() method to access the "test_collection" collection
27+
// start-access-collection-short
28+
$collection = $db->test_collection;
29+
// end-access-collection-short
30+
2231
// Explicitly creates the "example_collection" collection
2332
// start-create-collection
2433
$result = $client->test_database->createCollection('example_collection');
@@ -63,13 +72,15 @@
6372

6473
// end-collection-settings
6574

66-
// Instructs the library to prefer New York data center reads using a tag set
75+
// Instructs the library to prefer reads from secondary replica set members
76+
// located in New York but fall back to those in San Francisco
6777
// start-tag-set
6878
$readPreference = new ReadPreference(
6979
ReadPreference::RP_SECONDARY,
7080
[
7181
['dc' => 'ny'],
7282
['dc' => 'sf'],
83+
[],
7384
],
7485
);
7586

@@ -86,8 +97,8 @@
8697
$options = [
8798
'replicaSet' => 'repl0',
8899
'readPreference' => new ReadPreference(ReadPreference::RP_SECONDARY_PREFERRED),
89-
'localThresholdMS' => 35
100+
'localThresholdMS' => 35,
90101
];
91102

92103
$client = new Client('<connection string>', [], $options);
93-
// end-local-threshold
104+
// end-local-threshold

0 commit comments

Comments
 (0)