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

前端 未结 1 1715
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 09:24

I\'m trying to get results of db.stats() mongo shell command in my python code (for monitoring purposes).

But unlike for example serverStatus I can\'t do db.co

1条回答
  •  时光说笑
    2020-12-19 10:05

    The Javascript shell's stats command helper actually invokes a command named dbstats, which you can run from PyMongo using the Database.command method. The easiest way to find out what command a shell helper will run is to invoke the shell helper without parentheses -- this will print out the Javascript code it runs:

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

    As for why some commands have helpers and others do not, it's largely a question of preference, time, and perceived frequency of use by the driver authors. You can run any command by name with Database.command, which is just a convenience wrapper around db.$cmd.find_one. You can find a full list of commands at List of Database Commands. You can also submit a patch against PyMongo to add a helper method for commands you find that you need to invoke frequently but aren't supported by PyMongo yet.

    0 讨论(0)
提交回复
热议问题