==================================
MongoDB\\Collection::__construct()
==================================

.. default-domain:: mongodb

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

Definition
----------

.. phpmethod:: MongoDB\Collection::__construct()

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

   .. code-block:: php

      function __construct(
          MongoDB\Driver\Manager $manager,
          string $databaseName,
          string $collectionName,
          array $options = []
      )

   This constructor has the following parameters:

``$manager`` : :php:`MongoDB\Driver\Manager <class.mongodb-driver-manager>`
  The :php:`Manager <mongodb-driver-manager>` instance from the driver. The
  manager maintains connections between the driver and your MongoDB instances.

``$databaseName`` : string
  The name of the database.

``$collectionName`` : string
  The name of the collection.

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - codec
       - MongoDB\\Codec\\DocumentCodec
       - The default :doc:`codec </tutorial/codecs>` to use for collection
         operations.

         .. versionadded:: 1.17

     * - readConcern
       - :php:`MongoDB\Driver\ReadConcern <class.mongodb-driver-readconcern>`
       - The default read concern to use for collection operations. Defaults to
         the manager's read concern.

     * - readPreference
       - :php:`MongoDB\Driver\ReadPreference <class.mongodb-driver-readpreference>`
       - The default read preference to use for collection operations. Defaults
         to the manager's read preference.

     * - typeMap
       - array
       - Default :php:`type map
         <manual/en/mongodb.persistence.deserialization.php#mongodb.persistence.typemaps>`
         to apply to cursors, which determines how BSON documents are converted
         to PHP values. The library uses the following type map by default:

         .. code-block:: php

            [
                'array' => 'MongoDB\Model\BSONArray',
                'document' => 'MongoDB\Model\BSONDocument',
                'root' => 'MongoDB\Model\BSONDocument',
            ]

     * - writeConcern
       - :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
       - The default write concern to use for collection operations. Defaults
         to the manager's write concern.

Errors/Exceptions
-----------------

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

Behavior
--------

If you construct a Collection explicitly, the Collection inherits any options
from the :php:`MongoDB\Driver\Manager <class.mongodb-driver-manager>` object.
If you select the Collection from a :phpclass:`Client <MongoDB\Client>` or
:phpclass:`Database <MongoDB\Database>` object, the Collection inherits its
options from that object.

.. todo: add an example

See Also
--------

- :phpmethod:`MongoDB\Collection::withOptions()`
- :phpmethod:`MongoDB\Client::selectCollection()`
- :phpmethod:`MongoDB\Database::selectCollection()`
- :phpmethod:`MongoDB\Database::__get()`
