2012-07-25 8 views
6

kann ich die Statistiken von Mongo Shell alsMongoDB Shell db.stats() in PHP und Python

db.stats() 

oder

db.collection_name.stats() 

Wie kann ich sehen Statistik einer Datenbank oder eines sehen Sammlung von PHP und Python.

BEARBEITEN: Ich habe es in PHP getan, aber kann es immer noch nicht in Python tun.

Hilfe? Diese

+0

Sie eine Antwort akzeptieren soll, dass Ihre editierten Frage beantwortet;) –

Antwort

9

Dies ist, wie Sie es in Python tun, wenn Sie die PyMongo-Treiber verwenden:


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". 

Referenzen:

  • db.command()
  • MongoDB: how to get db.stats() from API
  • +0

    Danke für den Python-Code. – lovesh

    9

    ist, wie Sie es in PHP zu tun

    $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() 
    

    Aber wie dies in Python zu tun?

    +3

    1 Vielen Dank für den PHP-Code. –

    +0

    Thx OP für PHP-Code dafür. :) –

    0

    Der einfachste Weg, den ich gefunden habe dies mit einem Mongoengine Modell zu tun war:

    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()) 
    

    Diese transparenten Änderungen in der Sammlung ermöglichen sollte und Datenbank mongoengine Config-Einstellungen.

    0

    Dies ist PHP-Code dbStats Befehl mit neuen MongoDB Treiber auszuführen:

    $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']); 
    }