<commit_before>=====================================
MongoDB\\Database::selectCollection()
=====================================

.. default-domain:: mongodb

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

Definition
----------

.. phpmethod:: MongoDB\\Database::selectCollection()

   Selects a collection within the database.

   .. code-block:: php

      function selectCollection($collectionName, array $options = []): MongoDB\Collection

   This method has the following parameters:

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

   The ``$options`` parameter supports the following options:

   .. include:: /includes/apiargs/MongoDBDatabase-method-selectCollection-option.rst

Return Values
-------------

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

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

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

Behavior
--------

The selected collection inherits options such as read preference and type
mapping from the :phpclass:`Database <MongoDB\\Database>` object. Options may be
overridden via the ``$options`` parameter.

Example
-------

The following example selects the ``users`` collection in the ``test`` database:

.. code-block:: php

   <?php

   $db = (new MongoDB\Client)->test;

   $collection = $db->selectCollection('test', 'users');

The following example selects the ``users`` collection in the ``test``
database with a custom read preference:

.. code-block:: php<commit_msg>Fix example 1<commit_after>=====================================
MongoDB\\Database::selectCollection()
=====================================

.. default-domain:: mongodb

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

Definition
----------

.. phpmethod:: MongoDB\\Database::selectCollection()

   Selects a collection within the database.

   .. code-block:: php

      function selectCollection($collectionName, array $options = []): MongoDB\Collection

   This method has the following parameters:

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

   The ``$options`` parameter supports the following options:

   .. include:: /includes/apiargs/MongoDBDatabase-method-selectCollection-option.rst

Return Values
-------------

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

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

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

Behavior
--------

The selected collection inherits options such as read preference and type
mapping from the :phpclass:`Database <MongoDB\\Database>` object. Options may be
overridden via the ``$options`` parameter.

Example
-------

The following example selects the ``users`` collection in the ``test`` database:

.. code-block:: php

   <?php

   $db = (new MongoDB\Client)->test;

   $collection = $db->selectCollection('users');

The following example selects the ``users`` collection in the ``test``
database with a custom read preference:

.. code-block:: php
{'fraction_non_alphanumeric': 0.19052287581699345, 'fraction_numerical': 0.000980392156862745, 'mean_word_length': 3.738390092879257, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6984804', 'n_tokens_mistral': 835, 'n_tokens_neox': 783, 'n_words': 242}