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\Client::listDatabases()

Definition

MongoDB\Client::listDatabases

Returns information for all databases on the server.

function listDatabases(array $options = []): MongoDB\Model\DatabaseInfoIterator

This method has the following parameters:

Parameter Type Description
$options array Optional. An array specifying the desired options.

The $options parameter supports the following options:

Option Type Description
maxTimeMS integer Optional. The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.

Return Values

A traversable MongoDB\Model\DatabaseInfoIterator, which contains a MongoDB\Model\DatabaseInfo object for each database on the server.

Errors/Exceptions

MongoDB\Exception\UnexpectedValueException if the command response from the server was malformed.

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

MongoDB\Driver\Exception\RuntimeException for other errors at the driver level (e.g. connection errors).

Example

The following example lists all databases on the server:

<?php

$client = new MongoDB\Client;

foreach ($client->listDatabases() as $databaseInfo) {
    var_dump($databaseInfo);
}

The output would then resemble:

object(MongoDB\Model\DatabaseInfo)#4 (3) {
  ["name"]=>
  string(5) "local"
  ["sizeOnDisk"]=>
  float(65536)
  ["empty"]=>
  bool(false)
}
object(MongoDB\Model\DatabaseInfo)#7 (3) {
  ["name"]=>
  string(4) "test"
  ["sizeOnDisk"]=>
  float(32768)
  ["empty"]=>
  bool(false)
}

See Also