Find the real MongoDB command for pyMongo

August 13, 2010    mongo mongodb pymongo Python

I am in the middle of writing a MongoDB plugin for cacti using pymongo and ran into a little issue.

In the Mongo shell this works

> db.stats()
{
        "collections" : 19,
        "objects" : 145971,
        "dataSize" : 64785380,
        "storageSize" : 129544960,
        "numExtents" : 75,
        "indexes" : 69,
        "indexSize" : 25403392,
        "ok" : 1
}

So in pymongo I tried

info = db.command("stats")

Then I got the following traceback

Traceback (most recent call last):
  File "./get_mongodb_stats.py", line 68, in <module>
    main(sys.argv[1:])
  File "./get_mongodb_stats.py", line 39, in main
    get_stats(host, port)
  File "./get_mongodb_stats.py", line 57, in get_stats
    info = db.command("stats")
  File "/usr/lib/python2.6/site-packages/pymongo/database.py", line 306, in command
    (command, result["errmsg"]))

Then I remembered a nice part about the mongoshell. You can run the command without the () and see the function

> db.stats  
function () {
    return this.runCommand({dbstats:1});
}

So you do this in pymongo

info = db.command("dbstats")



comments powered by Disqus