MongoDB shell's db.stats() in php and python

Viewed 9895

I can see the statistics from Mongo shell as

db.stats()

or

db.collection_name.stats()

How do I view statistics of a database, or of a collection, from PHP and Python.

EDIT: I have done it in PHP but still cant do it in Python.

Help?

4 Answers

This is PHP code to execute dbStats command with new MongoDB driver:

$mongo = new \MongoDB\Driver\Manager('mongodb://localhost:27017');
$cmdstats = new \MongoDB\Driver\Command(['dbStats' => 1]);
$dbstats = $mongo->executeCommand('databaseName', $cmdstats);
$dbstats->setTypeMap(array(
    'array' => 'array',
    'document' => 'array',
    'root' => 'array'
));

// There must be only one item in $dbstats

foreach ($dbstats as $dbs)
{
    echo($dbs['dataSize']);
}
Related