Navigation
This version of the documentation is archived and no longer supported. To learn how to upgrade your version of PHP Library Manual, refer to the upgrade documentation.

MongoDB\Database::__get()

Definition

MongoDB\Database::__get

Select a collection within the database.

function __get($collectionName): MongoDB\Collection

This method has the following parameters:

Parameter Type Description
$collectionName string The name of the collection to select.

Return Values

A MongoDB\Collection object.

Behavior

The selected collection inherits options such as read preference and type mapping from the Database object. If you wish to override any options, use the MongoDB\Database::selectCollection method.

Note

To select collections whose names contain special characters, such as ., use complex syntax, as in $database->{'that.database'}.

Alternatively, MongoDB\Database::selectCollection supports selecting collections whose names contain special characters.

Examples

The following example selects the users and system.profile collections from the test database:

<?php

$db = (new MongoDB\Client)->test;

$users = $db->users;
$systemProfile = $db->{'system.profile'};