|
| 1 | +<?php |
| 2 | + |
| 3 | +$manager = new MongoDB\Manager("mongodb://localhost:27017"); |
| 4 | + |
| 5 | + |
| 6 | +/* List all available commands */ |
| 7 | + |
| 8 | +$listcommands = new MongoDB\Command(array("listCommands" => 1)); |
| 9 | +$retval = $manager->executeCommand("admin", $listcommands); |
| 10 | +$commands = $retval->getResponseDocument(); |
| 11 | + |
| 12 | +foreach($commands["commands"] as $command => $info) { |
| 13 | + echo $command, "\n -- ", trim(explode("\n", $info->help)[0]), "\n"; |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +echo "\n\n\n"; |
| 18 | + |
| 19 | + |
| 20 | +/* List all Databases */ |
| 21 | +$listdbs = new MongoDB\Command(array("listDatabases" => 1)); |
| 22 | +$retval = $manager->executeCommand("admin", $listdbs); |
| 23 | +$dbs = $retval->getResponseDocument(); |
| 24 | + |
| 25 | +foreach($dbs["databases"] as $dbinfo) { |
| 26 | + /* Print the Database name and its size on disk */ |
| 27 | + echo $dbinfo->name, " (", $dbinfo->sizeOnDisk / 1024 / 1024, "mb)\n"; |
| 28 | + |
| 29 | + /* List all the Collections within that database */ |
| 30 | + $listcollections = new MongoDB\Command(array("listCollections" => 1)); |
| 31 | + $retval = $manager->executeCommand($dbinfo->name, $listcollections); |
| 32 | + $collections = $retval->getResponseDocument(); |
| 33 | + |
| 34 | + foreach($collections["collections"] as $collinfo) { |
| 35 | + /* Retreieve the collection statistics for this collection */ |
| 36 | + $collstats = new MongoDB\Command(array("collStats" => $collinfo->name)); |
| 37 | + $retval = $manager->executeCommand($dbinfo->name, $collstats); |
| 38 | + $stats = $retval->getResponseDocument(); |
| 39 | + |
| 40 | + /* Print the collection name and document count */ |
| 41 | + echo " - ", $collinfo->name, " (" , $stats["count"], " document(s))\n"; |
| 42 | + |
| 43 | + /* Fetch all documents in this collection, but only retrieve the _id field */ |
| 44 | + $query = new MongoDB\Query(array(), array("projection" => array("_id" => 1))); |
| 45 | + $qi = $manager->executeQuery($dbinfo->name . "." . $collinfo->name, $query); |
| 46 | + foreach($qi as $document) { |
| 47 | + /* Not all documents have _id field (special internal collections for example) */ |
| 48 | + if (isset($document["_id"])) { |
| 49 | + echo " - _id: ", $document["_id"], "\n"; |
| 50 | + } else { |
| 51 | + echo " - (noid)\n"; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +/* The total size of all the databases */ |
| 57 | +echo "--\nTotal Size: ", $dbs["totalSize"] / 1024 / 1024, "mb\n"; |
| 58 | + |
| 59 | + |
| 60 | + |
0 commit comments