Database Manual / Reference / mongosh Methods / Databases

db.adminCommand() (mongosh method方法)

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Definition定义

db.adminCommand(command)

Provides a helper to run specified database commands against the admin database.提供一个帮助程序,用于对admin数据库运行指定的数据库命令

Parameter参数Type类型Description描述
commanddocument or string文档或字符串A database command, specified either in document form or as a string. 数据库命令,以文档形式或字符串形式指定。If specified as a string, the command cannot include any arguments.如果指定为字符串,则命令不能包含任何参数。

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. For information on Atlas support for all commands, see Unsupported Commands.所有MongoDB Atlas集群都支持此命令。有关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.adminCommand() runs commands against the admin database regardless of the database context in which it runs. The following commands are equivalent:admin数据库运行命令,而不管它在哪个数据库上下文中运行。以下命令等效:

db.getSiblingDB("admin").runCommand(<command>)

db.adminCommand(<command>)

For a list of available administrative database commands, see Administration Commands.有关可用管理数据库命令的列表,请参阅管理命令

Note

For a mongod or mongos running with authorization, the authorized user must have the appropriate privileges to run the database command. 对于使用授权运行的mongodmongos,授权用户必须具有运行数据库命令的适当权限。See the reference documentation for the command for more information on security requirements.有关安全要求的更多信息,请参阅该命令的参考文档。

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. 操作的逻辑时间。MongoDB使用逻辑时间来排序操作。Only for replica sets and sharded clusters.仅适用于副本集和分片集群。

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。

Examples示例

killOp

The following example uses the db.adminCommand() method to execute a killOp command to terminate an operation with opid 724. 以下示例使用db.adminCommand()方法执行killOp命令以终止opid 724的操作。killOp is an administrative command and must be run against the admin database.是一个管理命令,必须针对admin数据库运行。

db.adminCommand( { "killOp": 1, "op": 724 } )

renameCollection

The following example uses db.adminCommand() to execute the renameCollection administrative database command to rename the orders collection in the test database to orders-2016.以下示例使用db.adminCommand()执行renameCollection管理数据库命令,将test数据库中的orders集合重命名为orders-2016

db.adminCommand(
{
renameCollection: "test.orders",
to: "test.orders-2016"
}
)

createUser

The following example uses the db.adminCommand() method to create a user named bruce with the dbOwner role on the admin database.以下示例使用db.adminCommand()方法在admin数据库上创建一个名为bruce、具有dbOwner角色的用户。

Tip

You can use the passwordPrompt() method in conjunction with various user authentication management methods and commands to prompt for the password instead of specifying the password directly in the method or command call. 您可以将passwordPrompt()方法与各种用户身份验证管理方法和命令结合使用,以提示输入密码,而不是直接在方法或命令调用中指定密码。However, you can still specify the password directly as you would with earlier versions of the mongo shell.但是,您仍然可以像使用早期版本的mongo shell一样直接指定密码。

db.adminCommand(
{
createUser: "bruce",
pwd: passwordPrompt(), // or <cleartext password>
roles: [
{ role: "dbOwner", db: "admin" }
]
}
)