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

MongoDB\Client::listDatabases()

On this page

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

Returns information for all databases on the server.

function listDatabases(array $options = []): Iterator
$options : array

An array specifying the desired options.

Name
Type
Description
authorizedDatabases
boolean

A flag that determines which databases are returned based on the user privileges when access control is enabled. For more information, see the listDatabases command documentation.

For servers < 4.0.5, this option is ignored.

New in version 1.7.

comment
mixed

Enables users to specify an arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs.

This option is available since MongoDB 4.4 and will result in an exception at execution time if specified for an older server version.

New in version 1.13.

filter
array|object

A query expression to filter the list of databases.

You can specify a query expression for database fields (e.g. name, sizeOnDisk, empty).

New in version 1.3.

maxTimeMS
integer

The cumulative time limit in milliseconds for processing operations on the cursor. MongoDB aborts the operation at the earliest following interrupt point.

session

Client session to associate with the operation.

New in version 1.3.

An Iterator instance, which provides a MongoDB\Model\DatabaseInfo object for each database on the server.

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 extension level (e.g. connection errors).

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)
}

Back

listDatabaseNames()