Database Manual

Use Database Commands使用数据库命令

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 db.runCommand() method. 许多Driver为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返回一个包含以下字段的响应文档:

Field字段Description描述
<command result>Result fields specific to the command that ran.特定于所运行command的结果字段。
okA number that indicates if the command succeeded (1) or failed (0).一个数字,指示命令是成功(1)还是失败(0)。
operationTime

The logical time of the operation. MongoDB uses the logical time to order operations. Only for replica sets and sharded clusters.操作的逻辑时间。MongoDB使用逻辑时间来排序操作。仅适用于副本集和分片集群。

If the command does not generate an oplog entry, for example, a read operation, then the operation does not advance the logical clock. In this case, operationTime returns:如果该命令不生成oplog条目,例如读取操作,则该操作不会提前逻辑时钟。在这种情况下,operationTime返回:

For operations associated with causally consistent sessions, the MongoDB drivers use the logical time to automatically set the Read Operations and afterClusterTime period.对于与因果一致会话相关的操作,MongoDB驱动程序使用逻辑时间自动设置读取操作和afterClusterTime时段。

$clusterTime

A document that returns the signed cluster time. Cluster time is a logical time used for ordering of operations. Only for replica sets and sharded clusters. For internal use only.返回已签名群集时间的文档。集群时间是用于操作排序的逻辑时间。仅适用于副本集和分片集群。仅供内部使用。

The document contains the following fields:文档包含以下字段:

  • clusterTime: timestamp of the highest known cluster time for the member.:成员的最高已知集群时间的时间戳。
  • signature: a document that contains the hash of the cluster time and the id of the key used to sign the cluster time.:一个文档,其中包含集群时间的哈希值和用于对集群时间进行签名的键的id。