phpday 2021 digital edition


(PECL mongo >=1.3.0)

MongoClient::listDBsLists all of the databases available

This extension that defines this method is deprecated. Instead, the MongoDB extension should be used. There is no equivalent for this method in the new extension, but there is an alternative in the PHP library:


public MongoClient::listDBs ( ) : array


This function has no parameters.

Return Values

Returns an associative array containing three fields. The first field is databases, which in turn contains an array. Each element of the array is an associative array corresponding to a database, giving th database's name, size, and if it's empty. The other two fields are totalSize (in bytes) and ok, which is 1 if this method ran successfully.


Example #1 MongoClient::listDBs() example

Example demonstrating how to use listDBs and the returned data structure.


= new MongoClient();
$dbs $mongo->listDBs();


The above example will output something similar to:

    [databases] => Array
            [0] => Array
                    [name] => doctrine
                    [sizeOnDisk] => 218103808
                    [empty] =>

    [totalSize] => 218103808
    [ok] => 1

add a note add a note

User Contributed Notes 1 note

George Gombay
6 years ago
A "no frills" listing of all the databases present can be obtained by means of the simple following steps:

= new MongoClient("mongodb://localhost");
$dbases = $conn->listDBs();
$num = 0;
foreach (
$dbases['databases'] as $dbs) {
$dbname = $dbs['name'];
"<br> $num. $dbname";

On the assumption that you have three databases present, the foregoing will produce an output that will look similar to this:

1. local
2. members
3. test
To Top