Docs Menu
Docs Home
/ / /
PHP Library Manual
/ /

MongoDB\Client::selectDatabase()

On this page

  • Definition
  • Parameters
  • Return Values
  • Errors/Exceptions
  • Behavior
  • Example
  • See Also
MongoDB\Client::selectDatabase()

Selects a database on the server.

function selectDatabase(
string $databaseName,
array $options = []
): MongoDB\Database
$databaseName : string
The name of the database to select.
$options : array

An array specifying the desired options.

Name
Type
Description
readConcern
MongoDB\Driver\ReadConcern
The default read concern to use for database operations. Defaults to the client's read concern.
readPreference
The default read preference to use for database operations. Defaults to the client's read preference.
typeMap
array
The default type map to use for database operations. Defaults to the client's type map.
writeConcern
The default write concern to use for database operations. Defaults to the client's write concern.

A MongoDB\Database object.

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

The selected database inherits options such as read preference and type mapping from the Client object. Options may be overridden via the $options parameter.

The following example selects the test database:

<?php
$client = new MongoDB\Client;
$db = $client->selectDatabase('test');

The following examples selects the test database with a custom read preference:

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

Back

selectCollection()