==============================
MongoDB\\Client::__construct()
==============================

.. default-domain:: mongodb


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

Definition
----------

.. phpmethod:: MongoDB\\Client::__construct($uri, $uriOptions, $driverOptions)

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

   .. code-block:: php

      function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])

   :phpmethod:`MongoDB\\Client::__construct` has the following parameters:

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

Examples
--------

If you do not specify a ``$uri`` value, the driver connects to a
standalone :program:`mongod` on ``localhost`` via port ``27017``. The
following example demonstrates how to connect to a replica set
with a custom
read preference:

.. code-block:: php

   <?php

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

By default, the |php-library| deserializes BSON documents and arrays
as ``MongoDB\Model\BSONDocument`` and ``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>`.

.. code-block:: php

   <?php

   $client = new MongoDB\Client(
       null,
       [],
       [ 'typeMap' => [
           'root' => 'array', 'document' => 'array', 'array' => 'array'
         ],
       ]
   );
