=======================================
MongoDB\\Database::selectGridFSBucket()
=======================================

.. default-domain:: mongodb

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

Definition
----------

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

   Selects a GridFS bucket within the database.

   .. code-block:: php

      function selectGridFSBucket(array $options = []): MongoDB\GridFS\Bucket

Parameters
----------

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

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

     * - Name
       - Type
       - Description

     * - bucketName
       - string
       - The bucket name, which will be used as a prefix for the files and
         chunks collections. Defaults to ``"fs"``.

     * - chunkSizeBytes
       - integer
       - The chunk size in bytes. Defaults to ``261120`` (i.e. 255 KiB).

     * - codec
       - MongoDB\\Codec\\DocumentCodec
       - The default :doc:`codec </tutorial/codecs>` to use for bucket methods
         that return a file document (e.g. :phpmethod:`MongoDB\GridFS\Bucket::find()`).

         .. versionadded:: 1.17

     * - disableMD5
       - boolean
       - Whether to disable automatic MD5 generation when storing files.

         Defaults to ``false``. Only ``true`` will be supported in 2.0.

         .. versionadded:: 1.4

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

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

     * - typeMap
       - array
       - The default type map to use for bucket operations. Defaults to the
         database's type map.

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

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

A :phpclass:`MongoDB\GridFS\Bucket` object.

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

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

Behavior
--------

The selected bucket 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 default ``fs.files`` bucket in the ``test``
database:

.. code-block:: php

   <?php

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

   $bucket = $db->selectGridFSBucket();

The following example selects the custom ``images.files`` bucket in the ``test``
database with a custom read preference:

.. code-block:: php

   <?php

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

   $imagesBucket = $db->selectGridFSBucket([
       'bucketName' => 'images',
       'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'),
   ]);

See Also
--------

- :phpmethod:`MongoDB\GridFS\Bucket::__construct()`
