Database Manual / Reference / mongosh Methods / Databases

db.runCommand() (mongosh method方法)

Definition定义

db.runCommand(command, [options])

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文档或字符串Document or string that specifies the command. If specified as a string, db.runCommand() transforms the string into a document.指定命令的文档或字符串。如果指定为字符串,db.runCommand()会将字符串转换为文档。
optionsdocument文档Starting in mongosh 2.0, options that specify how mongosh runs the command.mongosh 2.0开始,指定mongosh如何运行命令的选项。
options.readPreference

read preference读取首选项

Read preference to run the command with. If not specified, defaults to primary. 读取首选项以运行命令。如果未指定,则默认为primarydb.runCommand() ignores any other read preference global configuration set, including using setReadPref() or by specifying readPreference in a connection string.db.runCommand()忽略任何其他读取首选项全局配置集,包括使用setReadPref()或在连接字符串中指定readPreference。

In mongosh 1.x, db.runCommand() does not take an options argument. To set the read preference, use Mongo.setReadPref() or specify readPreference in the connection string.mongosh 1x中,db.runCommand()不接受options参数。要设置读取首选项,请使用Mongo.setReadPref()或在连接字符串中指定readPreference

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

Compatibility兼容性

This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

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 before running these commands or use db.adminCommand().db.runCommand()在当前数据库的上下文中运行该命令。某些命令仅适用于admin数据库的上下文,您必须在运行这些命令之前更改db对象或使用db.adminCommand()

Read Preference读取首选项

Starting in mongosh 2.0, you can specify the read preference for a command with the options argument. If you do not specify a read preference, db.runCommand() defaults to primary.mongosh 2.0开始,您可以使用options参数指定命令的读取首选项。如果不指定读取首选项,db.runCommand()默认为primary

Warning

In mongosh 2.0, db.runCommand() ignores any other read preference global configuration set, including using Mongo.setReadPref() or by specifying readPreference in a connection string.mongosh 2.0中,db.runCommand()忽略任何其他读取首选项全局配置集,包括使用Mongo.setReadPref()或在连接字符串中指定readPreference

In mongosh 1.x, db.runCommand() does not take an options argument. db.runCommand() uses the read preference specified either by using Mongo.setReadPref() or by specifying readPreference in the connection string.mongosh 1x中,db.runCommand()不接受options参数。db.runCommand()使用通过Mongo.setReadPref()或在连接字符串中指定readPreference指定的读取首选项。

Examples示例

The following examples show how to use db.runCommand() to run database commands.以下示例显示了如何使用db.runCommand()运行数据库命令。

hello Command Without Specified Read Preference不用指定读取首选项的hello命令

This example shows how to use db.runCommand() to run the hello command.此示例显示了如何使用db.runCommand()运行hello命令。

db.runCommand( { hello: 1 } )

For details on output, see hello Output.有关输出的详细信息,请参阅hello输出

For details on read preference behavior, see Read Preference.有关读取首选项行为的详细信息,请参阅读取首选项

hello Command with readPreference: secondaryPreferred使用readPreference: secondaryPreferredhello命令

This example shows how to use db.runCommand() to run the hello command with the secondaryPreferred read preference.此示例显示了如何使用db.runCommand()运行具有secondaryPreferred读取首选项的hello命令。

db.runCommand( { hello: 1 }, { readPreference: "secondaryPreferred" } )

For details on output, see hello Output.有关输出的详细信息,请参阅hello输出

For details on read preference behavior, see Read Preference.有关读取首选项行为的详细信息,请参阅读取首选项

Response响应

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

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。