For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

MongoDB\Database::getGridFSBucket()

MongoDB\Database::getGridFSBucket()

Gets access to a GridFS bucket within the database. This method is an alias for MongoDB\Database::selectGridFSBucket() and will replace it in a future release.

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

An array specifying the desired options.

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 (255 KiB).

codec

MongoDB\Codec\DocumentCodec

The default Encode Data with Type Codecs to use for bucket methods that return a file document, such as MongoDB\GridFS\Bucket::find().

New in version 1.17.

readConcern

The default read concern to use for bucket operations. Defaults to the database's read concern.

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

The default write concern to use for bucket operations. Defaults to the database's write concern.

The getGridFSBucket() method returns a MongoDB\GridFS\Bucket object.

MongoDB\Exception\InvalidArgumentException for errors related to the parsing of parameters or options.

The selected bucket inherits options such as read preference and type mapping from the Database object. You can override options by using the $options parameter.

The following example gets access to the default fs.files bucket in the test database:

<?php
$db = (new MongoDB\Client)->test;
$bucket = $db->getGridFSBucket();

The following example gets access to the custom images.files bucket in the test database with a custom read preference:

<?php
$db = (new MongoDB\Client)->test;
$imagesBucket = $db->getGridFSBucket([
'bucketName' => 'images',
'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'),
]);