MongoDBDatabase-selectGridFSBucket.txt 1.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
=======================================
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

24
   This method has the following parameters:
25 26 27 28 29 30 31

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

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

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

32 33
Return Values
-------------
34

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

37 38 39 40 41
Errors/Exceptions
-----------------

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

42 43 44
Behavior
--------

45 46
The selected bucket inherits options such as read preference and type
mapping from the :phpclass:`Database <MongoDB\\Database>` object. Options may be
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
overridden via the ``$options`` parameter.

Example
-------

The following example selects the default ``fs.files`` bucket in the ``demo``
database:

.. code-block:: php

   <?php

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

   $bucket = $db->selectGridFSBucket();

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

.. code-block:: php

   <?php

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

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

77 78
See Also
--------
79

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