MongoDBClient__construct.txt 4.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
==============================
MongoDB\\Client::__construct()
==============================

.. default-domain:: mongodb

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

Definition
----------

16
.. phpmethod:: MongoDB\\Client::__construct()
17 18 19 20 21

   Constructs a new :phpclass:`Client <MongoDB\\Client>` instance.

   .. code-block:: php

22
      function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
23

24
   This constructor has the following parameters:
25 26 27

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

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

   .. include:: /includes/apiargs/MongoDBClient-method-construct-driverOptions.rst

32 33 34 35 36 37 38
Errors/Exceptions
-----------------

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

39 40 41 42 43 44 45 46 47 48
Behavior
--------

A :php:`MongoDB\\Driver\\Manager <mongodb-driver-manager>` is constructed
internally. Per the `Server Discovery and Monitoring
<https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#single-threaded-client-construction>`_
specification, :php:`MongoDB\\Driver\\Manager::__construct()
<mongodb-driver-manager.construct>` performs no I/O. Connections will be
initialized on demand, when the first operation is executed.

49 50 51
Examples
--------

52 53 54
Connecting to a Replica Set
~~~~~~~~~~~~~~~~~~~~~~~~~~~

55 56 57
If you do not specify a ``$uri`` value, the driver connects to a standalone
:program:`mongod` on ``127.0.0.1`` via port ``27017``. The following example
demonstrates how to connect to a replica set with a custom read preference:
58 59 60 61 62 63 64 65

.. code-block:: php

   <?php

   $client = new MongoDB\Client(
       'mongodb://rs1.example.com,rs2.example.com/?replicaSet=myReplicaSet',
       [
66
           'readPreference' => 'secondaryPreferred',
67 68 69
       ]
   );

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
Connecting with SSL and Authentication
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following example demonstrates how to connect to a MongoDB replica set with
SSL and authentication, as is used for `MongoDB Atlas
<https://cloud.mongodb.com/?jmp=docs>`_:

.. code-block:: php

   <?php

   $client = new MongoDB\Client(
       'mongodb://myUsername:myPassword@rs1.example.com,rs2.example.com/?ssl=true&replicaSet=myReplicaSet&authSource=admin'
   );

Alternatively, the authentication credentials and URI parameters may be
specified in the constructor's ``$uriOptions`` parameter:

.. code-block:: php

   <?php

   $client = new MongoDB\Client(
       'mongodb://rs1.example.com,rs2.example.com/'
       [
           'username' => 'myUsername',
           'password' => 'myPassword',
           'ssl' => true,
           'replicaSet' => 'myReplicaSet',
           'authSource' => 'admin',
       ],
   );

The driver supports additional :php:`SSL options
<mongodb-driver-manager.construct#mongodb-driver-manager.construct-driveroptions>`,
which may be specified in the constructor's ``$driverOptions`` parameter. Those
options are covered in the :php:`MongoDB\\Driver\\Manager::__construct()
<mongodb-driver-manager.construct>` documentation.

Specifying a Custom Type Map
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

112
By default, the |php-library| deserializes BSON documents and arrays
113 114 115 116
as :phpclass:`MongoDB\\Model\\BSONDocument` and
:phpclass:`MongoDB\\Model\\BSONArray` objects, respectively. The following
example demonstrates how to have the library unserialize everything as a PHP
array, as was done in the legacy :php:`mongo extension <mongo>`.
117 118 119 120 121 122 123 124

.. code-block:: php

   <?php

   $client = new MongoDB\Client(
       null,
       [],
125 126 127 128 129 130
       [
           'typeMap' => [
               'root' => 'array',
               'document' => 'array',
               'array' => 'array',
           ],
131 132
       ]
   );
133

134 135
See Also
--------
136

137 138
- :php:`MongoDB\\Driver\\Manager::__construct()
  <mongodb-driver-manager.construct>`
139 140
- :manual:`Connection String URI Format </reference/connection-string>` in the
  MongoDB manual
141 142 143
- `Server Discovery and Monitoring
  <https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#single-threaded-client-construction>`_
  specification