MongoDB\Client::getCollection()
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
MongoDB\Client::getCollection()
Definition
Gets access to a collection on the server. This method is an alias for [MongoDB\Client::selectCollection()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbclient-selectcollection.md#mongodb-phpmethod-phpmethod.MongoDB-Client--selectCollection-- and will replace it in a future release.
function getCollection(
string $databaseName,
string $collectionName,
array $options = []
): MongoDB\Collection
Parameters
$databaseName : string
The name of the database containing the collection to access.
$collectionName : string
The name of the collection to access.
$options : array
An array specifying the desired options.
| Name | Type | Description |
|---|---|---|
| codec | MongoDB\Codec\DocumentCodec | The default [Encode Data with Type Codecs](https://www.mongodb.com/docs/php-library/current/data-formats/custom-types/codecs.md#std-label-php-codecs to use for collection operations. New in version 1.17 |
| readConcern | MongoDB\Driver\ReadConcern | The default read concern to use for collection operations. Defaults to the client's read concern. |
| readPreference | MongoDB\Driver\ReadPreference | The default read preference to use for collection operations. Defaults to the client's read preference. |
| typeMap | array | The default type map to use for collection operations. Defaults to the client's type map. |
| writeConcern | MongoDB\Driver\WriteConcern | The default write concern to use for collection operations. Defaults to the client's write concern. |
Return Values
A [MongoDB\Collection](https://www.mongodb.com/docs/php-library/current/reference/class/mongodbcollection.md#mongodb-phpclass-phpclass.MongoDB-Collection object.
Errors/Exceptions
[MongoDB\Exception\InvalidArgumentException](https://www.mongodb.com/docs/php-library/current/reference/exception-classes.md#mongodb-phpclass-phpclass.MongoDB-Exception-InvalidArgumentException for errors related to the parsing of parameters or options.
Behavior
The selected collection inherits options such as read preference and type mapping from the [Client](https://www.mongodb.com/docs/php-library/current/reference/class/mongodbclient.md#mongodb-phpclass-phpclass.MongoDB-Client object. Options may be overridden by using the $options parameter.
Example
The following example gets access to the users collection in the test database:
<?php
$client = new MongoDB\Client;
$collection = $client->getCollection('test', 'users');
The following example gets access to the users collection in the test database with a custom read preference:
<?php
$client = new MongoDB\Client;
$collection = $client->getCollection(
'test',
'users',
[
'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'),
]
);
See Also
-
[
MongoDB\Collection::__construct()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbcollection__construct.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--__construct-- -
[
MongoDB\Database::getCollection()](https://www.mongodb.com/docs/php-library/current/reference/method/mongodbdatabase-getcollection.md#mongodb-phpmethod-phpmethod.MongoDB-Database--getCollection--
