Docs HomeMongoDB Manual

db.runCommand()

Definition定义

db.runCommand(command)

Provides a helper to run specified database commands. 提供帮助程序以运行指定的数据库命令This is the preferred method to issue database commands, as it provides a consistent interface between the shell and drivers.这是发出数据库命令的首选方法,因为它在shell和驱动程序之间提供了一致的接口。

Parameter参数Type类型Description描述
commanddocument or string"A database command, specified either in document form or as a string. 数据库命令,以文档形式或字符串形式指定。If specified as a string, db.runCommand() transforms the string into a document."如果指定为字符串,db.runCommand()会将字符串转换为文档。”

To specify a time limit in milliseconds, see Terminate Running Operations.要指定以毫秒为单位的时间限制,请参阅终止正在运行的操作

Behavior行为

db.runCommand() runs the command in the context of the current database. 在当前数据库的上下文中运行该命令。Some commands are only applicable in the context of the admin database, and you must change your db object to before running these commands or use db.adminCommand().有些命令只适用于admin数据库的上下文,在运行这些命令或使用db.adminCommand()之前,必须将db对象更改为admin数据库。

Response响应

The method returns a response document that contains the following fields:该方法返回一个包含以下字段的响应文档:

Field字段Description描述
<command result>Result fields specific to the command that was run.特定于运行的command的结果字段。
okA number that indicates whether the command has succeeded (1) or failed (0).一个数字,指示命令是成功(1)还是失败(0)。
operationTimeThe logical time of the operation. MongoDB uses the logical time to order operations. 操作的逻辑时间。MongoDB使用逻辑时间来订购操作。Only for replica sets and sharded clusters.仅适用于复制集和分片集群。
If the command does not generate an oplog entry, e.g. a read operation, then the operation does not advance the logical clock. 如果命令没有生成oplog条目,例如读取操作,则该操作不会提前逻辑时钟。In this case, operationTime returns: 在这种情况下,operationTime返回: For operations associated with causally consistent sessions, MongoDB drivers use this time to automatically set the Read Operations and afterClusterTime. 对于与因果一致会话相关联的操作,MongoDB驱动程序使用此时间自动设置读取操作和afterClusterTime
$clusterTimeA 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。