@@ -45,6 +45,24 @@ The following example accesses a database named ``test_database``:
45
45
:start-after: start-access-database
46
46
:end-before: end-access-database
47
47
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
+
48
66
.. _php-db-coll-access-collection:
49
67
50
68
Access a Collection
@@ -71,6 +89,20 @@ The following example accesses a collection named ``test_collection`` by using t
71
89
MongoDB implicitly creates the collection when you first insert data
72
90
into it.
73
91
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
+
74
106
.. _php-db-coll-create-collection:
75
107
76
108
Create a Collection
@@ -100,7 +132,7 @@ You can query for a list of collections in a database by calling the
100
132
cursor containing all collections in the database and their associated metadata.
101
133
102
134
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`
104
136
and :ref:`php-db-coll-create-collection` examples:
105
137
106
138
.. io-code-block::
@@ -129,9 +161,6 @@ and :ref:`php-db-coll-create-collection` examples:
129
161
...
130
162
)
131
163
132
- For more information about iterating over a cursor, see the :ref:`php-cursors`
133
- guide.
134
-
135
164
Delete a Collection
136
165
-------------------
137
166
@@ -164,22 +193,23 @@ read and write operations on a replica set by setting a **read concern** and a
164
193
**write concern**.
165
194
166
195
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.
171
201
172
202
To change the read preference, read concern, and write concern of your database
173
203
or collection, set the following options in the array parameter:
174
204
175
205
- ``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>`
177
207
in the extension API documentation.
178
208
- ``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>`
180
210
in the extension API documentation.
181
211
- ``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>`
183
213
in the extension API documentation.
184
214
185
215
.. tip::
@@ -217,8 +247,11 @@ array to ``selectCollection()``:
217
247
:start-after: start-collection-settings
218
248
:end-before: end-collection-settings
219
249
250
+ Advanced Read Configurations
251
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
252
+
220
253
Tag Sets
221
- ~~~~~~~~
254
+ ````````
222
255
223
256
In {+mdb-server+}, you can apply key-value :manual:`tags
224
257
</core/read-preference-tags/>` to replica-set
@@ -243,7 +276,7 @@ center (``'dc' => 'sf'``):
243
276
:end-before: end-tag-set
244
277
245
278
Local Threshold
246
- ~~~~~~~~~~~~~~~
279
+ ```````````````
247
280
248
281
If multiple replica-set members match the read preference and tag sets you specify,
249
282
the {+php-library+} reads from the nearest replica-set members, chosen according to
0 commit comments