Online Snippets Library

Show all the databases in Mongodb

To Show all the databases in Mongodb, we will use MongoClient::listDBs().

Example

<?php
$m = new MongoClient('mongodb://localhost:27017');
$dbases = $m->listDBs(); 
foreach ($dbases['databases'] as $dbs) {
          $dbname = $dbs['name'];
         echo "<br> $dbname";
     }
?>
OutPut
localdb
productiondb
testdb