MongoDB:如何从API获取db.stats() [英] MongoDB: how to get db.stats() from API

查看:64
本文介绍了MongoDB:如何从API获取db.stats()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在我的python代码中获取db.stats()mongo shell命令的结果(出于监视目的).

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

但是与例如serverStatus不同,我不能执行db.command('stats').我无法在mongodb文档中找到任何等效的API.我还尝试过使用db.$cmd进行变体,但是没有一个奏效.

But unlike for example serverStatus I can't do db.command('stats'). I was not able to find any API equivalent in mongodb docs. I've also tried variations with db.$cmd but none of that worked.

所以

小问题:如何在我的python代码中获得db.stats()的结果(连接/对象的数量,数据和索引的大小等)?

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

更大的问题:任何人都可以解释为什么某些shell命令可以从API轻松访问而另一些则不能吗?这很烦人:某些与管理员相关的工具可通过db.$cmd.sys访问,某些可通过db.command访问,某些可通过...访问?有这种情况的标准或解释吗?

Bigger question: can anyone explain why some of shell commands are easily accessible from API, while others are not? It's very annoying: some admin-related tools are accessible via db.$cmd.sys, some via db.command, some via ...? Is there some standard or explanation of this situation?

PS:mongodb 2.0.2,pymongo 2.1.0,python 2.7

PS: mongodb 2.0.2, pymongo 2.1.0, python 2.7

推荐答案

Javascript shell的stats命令助手实际上调用了名为dbstats的命令,您可以使用

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});
}

至于为什么有些命令有助手,而有些则没有,这很大程度上是驱动程序作者的偏好,时间和使用频率的问题.您可以使用Database.command按名称运行任何命令,这只是db.$cmd.find_one的便利包装.您可以在数据库命令列表中找到命令的完整列表. .您还可以提交针对PyMongo的补丁,以为您发现的命令添加辅助方法需要频繁调用,但PyMongo尚不支持.

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.

这篇关于MongoDB:如何从API获取db.stats()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆