MongoDBClient-selectDatabase.txt 1.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
=================================
MongoDB\\Client::selectDatabase()
=================================

.. default-domain:: mongodb

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

Definition
----------

16 17 18
.. phpmethod:: MongoDB\\Client::selectDatabase()

   Selects a database on the server.
19 20 21

   .. code-block:: php

22
      function selectDatabase($databaseName, array $options = []): MongoDB\Database
23

24
   This method has the following parameters:
25 26 27

   .. include:: /includes/apiargs/MongoDBClient-method-selectDatabase-param.rst

28
   The ``$options`` parameter supports the following options:
29 30 31

   .. include:: /includes/apiargs/MongoDBClient-method-selectDatabase-option.rst

32 33
Return Values
-------------
34

35
A :phpclass:`MongoDB\\Database` object.
36

37 38 39 40 41
Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-invalidargumentexception.rst

42 43
Behavior
--------
44

45 46 47
The selected database inherits options such as read preference and type mapping
from the :phpclass:`Client <MongoDB\\Client>` object. Options may be overridden
via the ``$options`` parameter.
48 49 50 51

Example
-------

52
The following example selects the ``test`` database:
53 54 55 56 57 58 59

.. code-block:: php

   <?php

   $client = new MongoDB\Client;

60
   $db = $client->selectDatabase('test');
61

62
The following examples selects the ``test`` database with a custom read
63 64 65 66 67 68 69 70 71
preference:

.. code-block:: php

   <?php

   $client = new MongoDB\Client;

   $db = $client->selectDatabase(
72
       'test',
73 74 75 76 77
       [
           'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
       ]
   );

78 79
See Also
--------
80

81 82
- :phpmethod:`MongoDB\\Client::__get()`
- :phpmethod:`MongoDB\\Database::__construct()`