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从mongoshruns the command.mongosh2.0开始,指定mongosh如何运行命令的选项。options.readPreferenceRead preference to run the command with. If not specified, defaults to读取首选项以运行命令。如果未指定,则默认为primary.primary。db.runCommand()ignores any other read preference global configuration set, including usingsetReadPref()or by specifyingreadPreferencein a connection string.db.runCommand()忽略任何其他读取首选项全局配置集,包括使用setReadPref()或在连接字符串中指定readPreference。In mongosh 1.x,在db.runCommand()does not take anoptionsargument. To set the read preference, useMongo.setReadPref()or specifyreadPreferencein the connection string.mongosh1x中,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: secondaryPreferred的hello命令
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:该方法返回一个包含以下字段的响应文档:
| <command result> | command that ran.command的结果字段。 |
ok | 1) or failed (0).1)还是失败(0)。 |
operationTime |
|
$clusterTime |
|