Find the real MongoDB command for pyMongo

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

About mike
Currently works for OpenSky as a Senior Linux Admin. He has a wonderful wife Thanuja and 2 great dogs. His major side project is Photoblog.

Comments

One Response to “Find the real MongoDB command for pyMongo”

Trackbacks

Check out what others are saying about this post...
  1. ehcache.net says:

    Find the real MongoDB command for pyMongo…

    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…



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!