Docs Menu

Docs HomePHP Library Manual

MongoDB\Database::__get()

On this page

  • Definition
  • Parameters
  • Return Values
  • Behavior
  • Examples
  • See Also
MongoDB\Database::__get()

Select a collection within the database.

function __get(string $collectionName): MongoDB\Collection
$collectionName : string
The name of the database to select.

A MongoDB\Collection object.

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.

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'};
←  MongoDB\Database::__construct()MongoDB\Database::aggregate() →