MongoDBDatabase__get.txt 1.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
==========================
MongoDB\\Database::__get()
==========================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16
.. phpmethod:: MongoDB\\Database::__get()
17

18
   Select a collection within the database.
19 20 21 22 23

   .. code-block:: php

      function __get($collectionName): MongoDB\Collection

24
   This method has the following parameters:
25 26 27

   .. include:: /includes/apiargs/MongoDBDatabase-method-get-param.rst

28 29
Return Values
-------------
30

31
A :phpclass:`MongoDB\\Collection` object.
32

33 34 35
Behavior
--------

36 37 38 39
The selected collection inherits options such as read preference and type
mapping from the :phpclass:`Database <MongoDB\\Database>` object. If you wish to
override any options, use the :phpmethod:`MongoDB\\Database::selectCollection`
method.
40 41

.. note::
42

43 44
   To select collections whose names contain special characters, such as
   ``.``, use complex syntax, as in ``$database->{'that.database'}``.
45

46
   Alternatively, :phpmethod:`MongoDB\\Database::selectCollection` supports
47
   selecting collections whose names contain special characters.
48 49 50 51 52

Examples
--------

The following example selects the ``users`` and ``system.profile``
53
collections from the ``test`` database:
54 55 56 57 58

.. code-block:: php

   <?php

59
   $db = (new MongoDB\Client)->test;
60 61 62 63

   $users = $db->users;
   $systemProfile = $db->{'system.profile'};

64 65
See Also
--------
66

67 68 69
- :phpmethod:`MongoDB\\Database::selectCollection()`
- :phpmethod:`MongoDB\\Client::selectCollection()`
- :php:`Property Overloading <oop5.overloading>` in the PHP Manual