MongoDB\Database::selectCollection()
Definition
Parameters
$collectionName
: string- The name of the collection to select.
$options
: arrayAn array specifying the desired options.
NameTypeDescriptioncodecMongoDB\Codec\DocumentCodecThe default codec to use for collection operations.
New in version 1.17.
readConcernThe default read concern to use for collection operations. Defaults to the database's read concern.readPreferenceThe default read preference to use for collection operations. Defaults to the database's read preference.typeMaparrayThe default type map to use for collection operations. Defaults to the database's type map.writeConcernThe default write concern to use for collection operations. Defaults to the database's write concern.
Return Values
A MongoDB\Collection
object.
Errors/Exceptions
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 Database
object. Options may be
overridden via the $options
parameter.
Example
The following example selects the users
collection in the test
database:
$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:
$db = (new MongoDB\Client)->test; $users = $db->selectCollection( 'users', [ 'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'), ] );