MongoDB shell db.stats () in php and python

I can see statistics from shell Mongo as

db.stats() 

or

 db.collection_name.stats() 

How to view database statistics or collections from PHP and Python.

EDIT : I did it in PHP, but still can't do it in Python.

reference

+6
source share
4 answers

Here's how you do it in Python if you use the PyMongo driver:

 connection = pymongo.Connection(host = "127.0.0.1", port = 27017) db = connection["test_db"] test_collection = db["test_collection"] db.command("dbstats") # prints database stats for "test_db" db.command("collstats", "test_collection") # prints collection-level stats for "test_collection" under "test_db". 

References:

db.command ()

MongoDB: how to get db.stats () from API
+9
source

This is how you do it in PHP

 $con= new Mongo() $stats=$con->dbName->command(array('dbStats' => 1)); // for db.stats() $stats=$con->dbName->command(array('collStats' => 'collection_name')); // for db.collection_name.stats() 

But how to do it in python?

+9
source

The easiest way I found this with the Mongoengine model was as follows:

 import mongoengine from models import MyModel connection = mongoengine.connection.get_connection() db = connection[MyModel._get_db().name] stats = db.command("collstats", MyModel._get_collection_name()) 

This should allow transparent changes to the collection and database using the mongoengine configuration settings.

0
source

This is the PHP code to execute the dbStats with the 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']); } 
0
source

Source: https://habr.com/ru/post/921290/


All Articles