如何通过查询特殊的$ cmd集合来运行MongoDB命令? [英] How can I run MongoDB commands by querying the special $cmd collection?

查看:393
本文介绍了如何通过查询特殊的$ cmd集合来运行MongoDB命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现MongoDB驱动程序(实际上,我并不是从头开始实现它,而是在改进现有的小型驱动程序,但这无关紧要).向MongoDB发出命令似乎只是通过对$cmd集合的特殊查询来执行. MongoDB词汇表中对此进行了说明,

I'm trying to implement a MongoDB driver (actually I'm not implementing it from scratch, I'm improving an existing small one, but that's irrelevant). Issuing commands to MongoDB appears to simply be performed via special queries to the $cmd collection. This is described in the MongoDB glossary as follows:

$ cmd

一个特殊的虚拟集合,公开了MongoDB的数据库命令.要使用数据库命令,请参阅发出命令.

$cmd

A special virtual collection that exposes MongoDB’s database commands. To use database commands, see Issue Commands.

好的.那我该怎么做呢?如何查看使用数据库命令?

Okay. So how do I do that? How about looking at Use Database Commands?

许多驱动程序为db.runCommand()方法提供了等效项.在内部,使用db.runCommand()运行命令等效于针对$cmd集合的特殊查询.

Many drivers provide an equivalent for the db.runCommand() method. Internally, running commands with db.runCommand() is equivalent to a special query against the $cmd collection.

嗯,好的.那没有帮助.我正在编写一个驱动程序,而不使用一个驱动程序.

Um, okay. That's not helpful. I'm writing a driver, not using one.

是否有文档说明如何实际实现runCommand功能?对$cmd的查询应该如何工作?

Is there documentation somewhere for how to actually implement runCommand functionality? How are the queries against $cmd supposed to work?

推荐答案

对$ cmd的查询应该如何工作?

How are the queries against $cmd supposed to work?

数据库命令是对特殊集合$cmd的查询, 查询选择器在其中定义命令本身.因此,等效于使用db.runCommand({isMaster: 1})运行命令的查询是:

A database command is a query on a special collection, $cmd, where the query selector defines the command itself. So, query equivalent for running a command using db.runCommand({isMaster: 1}) is:

db.$cmd.findOne({isMaster: 1})

在我的机器上,它们都产生了以下结果:

In my machine, they both generated the following result:

{
        "ismaster" : true,
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 1000,
        "localTime" : ISODate("2016-04-22T12:46:02.378Z"),
        "maxWireVersion" : 4,
        "minWireVersion" : 0,
        "ok" : 1
}

这篇关于如何通过查询特殊的$ cmd集合来运行MongoDB命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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