MongoDB\Client::selectDatabase()
Definition
Parameters
$databaseName
: string- The name of the database to select.
$options
: arrayAn array specifying the desired options.
NameTypeDescriptionreadConcernThe default read concern to use for database operations. Defaults to the client's read concern.readPreferenceThe default read preference to use for database operations. Defaults to the client's read preference.typeMaparrayThe default type map to use for database operations. Defaults to the client's type map.writeConcernThe default write concern to use for database operations. Defaults to the client's write concern.
Return Values
A MongoDB\Database
object.
Errors/Exceptions
MongoDB\Exception\InvalidArgumentException
for errors related to
the parsing of parameters or options.
Behavior
The selected database inherits options such as read preference and type mapping
from the Client
object. Options may be overridden
via the $options
parameter.
Example
The following example selects the test
database:
$client = new MongoDB\Client; $db = $client->selectDatabase('test');
The following examples selects the test
database with a custom read
preference:
$client = new MongoDB\Client; $db = $client->selectDatabase( 'test', [ 'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'), ] );