MongoDB: how to get db.stats () from API

I am trying to get the results of the db.stats () mongo shell command in my Python code (for monitoring purposes).

But unlike, for example, serverStatus, I cannot do db.command('stats') . I could not find the equivalent of the API in mongodb docs. I also tried options with db.$cmd , but none of this worked.

So,

A small question: how can I get the results of db.stats() (number of connections / objects, size of data and indexes, etc.) in my python code?

The big question is: can someone explain why some shell commands are easily accessible from the API and others not? This is very annoying: some admin-related tools are available through db.$cmd.sys , some through db.command , some through ...? Is there any standard or explanation for this situation?

PS: mongodb 2.0.2, pymongo 2.1.0, python 2.7

+5
source share
1 answer

The stats Javascript shell helper actually calls a command called dbstats , which you can run from PyMongo using the Database.command method. The easiest way to find out which command will be executed using the shell helper is to call the shell helper without parentheses - this will output the Javascript code that it runs:

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

As to why some teams have assistants, while others do not, this is largely a matter of preference, time, and the authors ’expected frequency of use of the driver. You can run any command by name using Database.command , which is a handy wrapper around db.$cmd.find_one . A complete list of commands can be found in the Database Command List . You can also send a patch against PyMongo to add a helper method for commands that you find that you need to call often but not yet supported by PyMongo.

+10
source

Source: https://habr.com/ru/post/921292/


All Articles