Last active
August 29, 2015 14:11
-
-
Save jaymecd/44420d3103ad63b30ba2 to your computer and use it in GitHub Desktop.
Mongo simple php starts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
try { | |
$mongo = new MongoClient('mongodb://USER:PASS@HOST:PORT', array('db'=>'admin')); | |
$db = $mongo->selectDB('DBNAME'); | |
$collections = []; | |
foreach ($db->listCollections() as $coll) { | |
$collections[$coll->getName()] = $coll->count(); | |
} | |
echo sprintf('Mongo [%s] connected', $host), PHP_EOL; | |
echo PHP_EOL; | |
ksort($collections); | |
$len = array_reduce(array_keys($collections), function ($carry, $key) { return max($carry, strlen($key)); }, 0) + 2; | |
foreach ($collections as $name => $count) { | |
echo sprintf('%\'.-'. $len .'s : %s', $name.' ', number_format($count, 0, ',', '.')), PHP_EOL; | |
} | |
echo PHP_EOL; | |
} catch (MongoConnectionException $e) { | |
echo $e, PHP_EOL; | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment