The MongoDB command interface provides access to all non CRUD database operations. Fetching server statistics, initializing a replica set, and running an aggregation pipeline or map-reduce job are all accomplished with commands.MongoDB命令接口提供对所有非CRUD数据库操作的访问。获取服务器统计信息、初始化副本集以及运行聚合管道或映射缩减作业都是通过命令完成的。
See Database Commands for list of all commands sorted by function.有关按函数排序的所有命令的列表,请参阅数据库命令。
Database Command Form数据库命令表
You specify a command first by constructing a standard BSON document whose first key is the name of the command. 首先,您可以通过构造一个标准的BSON文档来指定命令,该文档的第一个键是命令的名称。For example, specify the hello command using the following BSON document:
{ hello: 1 }Issue Commands发出命令
mongosh provides a helper method for running commands called db.runCommand(). mongosh提供了一个名为db.runCommand()的辅助方法来运行命令。The following operation in mongosh runs the previous command:mongosh中的以下操作运行前面的命令:
db.runCommand( { hello: 1 } )
Many Drivers provide an equivalent for the 许多Driver为db.runCommand() method. db.runCommand()方法提供了等效的驱动程序。Internally, running commands with 在内部,使用db.runCommand() is equivalent to a special query against the $cmd collection.db.runCommand()运行命令相当于对$cmd集合进行特殊查询。
Many common commands have their own shell helpers or wrappers in 许多常用命令在mongosh and drivers, such as the db.hello() method in mongosh.mongosh和驱动程序中都有自己的shell助手或包装器,例如mongosh中的db.hello()方法。
You can use the 您可以使用maxTimeMS option to specify a time limit for the execution of a command, see Terminate a Command for more information on operation termination.maxTimeMS选项指定执行命令的时间限制,有关操作终止的更多信息,请参阅终止命令。
admin Database Commands数据库命令
You must run some commands on the admin database. Normally, these operations resemble the following:您必须在admin数据库上运行一些命令。通常,这些操作类似于以下内容:
use admin
db.runCommand( {buildInfo: 1} )
However, there's also a command helper that automatically runs the command in the context of the 但是,还有一个命令助手可以在admin database:admin数据库的上下文中自动运行命令:
db.adminCommand( {buildInfo: 1} )Command Responses命令响应
For all commands, MongoDB returns a response document that contains the following fields:对于所有命令,MongoDB返回一个包含以下字段的响应文档:
| <command result> | command that ran.command的结果字段。 |
ok | 1) or failed (0).1)还是失败(0)。 |
operationTime |
|
$clusterTime |
|