Skip to content

Instantly share code, notes, and snippets.

@jaymecd
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaymecd/44420d3103ad63b30ba2 to your computer and use it in GitHub Desktop.
Save jaymecd/44420d3103ad63b30ba2 to your computer and use it in GitHub Desktop.
Mongo simple php starts
#!/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