Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run raw mongodb commands from pymongo

In a mongo command line I can run

db.my_collection.stats()

I need to get my collections stats from Python so I tried

from pymongo import MongoClient

client = MongoClient()
db = client.test_database

collection = db.test_collection

collection.stats()

But I get

TypeError: 'Collection' object is not callable. 
If you meant to call the 'stats' method on a 'Collection' object it is failing because no such method exists.

This is because pymongo does not support this method. How do I send raw mongoDB commands to mongo through Python?

like image 700
Dr Manhattan Avatar asked Dec 04 '14 15:12

Dr Manhattan


1 Answers

from pymongo import MongoClient

client = MongoClient()

db = client.test_database

print(db.command("collstats", "test_collection"))
like image 105
Chris Nilsson Avatar answered Sep 19 '22 12:09

Chris Nilsson