Docs Menu

Docs HomePHP Library Manual

MongoDB\Client::selectCollection()

On this page

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

Selects a collection on the server.

function selectCollection(
string $databaseName,
string $collectionName,
array $options = []
): MongoDB\Collection
$databaseName : string
The name of the database containing the collection to select.
$collectionName : string
The name of the collection to select.
$options : array

An array specifying the desired options.

Name
Type
Description
readConcern
MongoDB\Driver\ReadConcern
The default read concern to use for collection operations. Defaults to the client's read concern.
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
The default write concern to use for collection operations. Defaults to the client's write concern.

A MongoDB\Collection object.

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

The selected collection 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 users collection in the test database:

<?php
$client = new MongoDB\Client;
$collection = $client->selectCollection('test', 'users');

The following example selects the users collection in the test database with a custom read preference:

<?php
$client = new MongoDB\Client;
$collection = $client->selectCollection(
'test',
'users',
[
'readPreference' => new MongoDB\Driver\ReadPreference('primaryPreferred'),
]
);
←  MongoDB\Client::removeSubscriber()MongoDB\Client::selectDatabase() →