Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 871
lovesh Avatar asked Jul 25 '12 11:07

lovesh


2 Answers

This is how you do it in Python if you are using 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
  • like image 154
    Sagar Hatekar Avatar answered Sep 18 '22 14:09

    Sagar Hatekar


    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 this in python?

    like image 42
    lovesh Avatar answered Sep 17 '22 14:09

    lovesh