db.adminCommand()

On this page本页内容

Definition定义

db.adminCommand(command)

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

Parameter参数Type类型Description描述
command document 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.如果指定为字符串,则命令不能包含任何参数。

Behavior行为

db.adminCommand runs commands against the admin database regardless of the database context in which it runs. admin数据库运行命令,而不考虑其运行的数据库上下文。The following commands are equivalent:以下命令是等效的:

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. 对于使用authorization运行的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描述
Result fields specific to the command特定于命令的结果字段  
ok A number that indicates whether the command has succeeded (1) or failed (0).指示命令是成功(1)还是失败(0)的数字。
operationTime

The logical time of the performed operation, represented in MongoDB by the timestamp from the oplog entry. 执行操作的逻辑时间,在MongoDB中由oplog条目的时间戳表示。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.

New in version 3.6.版本3.6中的新功能。

$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的文档。

New in version 3.6.版本3.6中的新功能。

Examples示例

killOp

The following example uses the db.adminCommand() method to execute a killOp command to terminate an operation with opid 724. killOp is an administrative command and must be run against the admin database.

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",
     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()方法创建一个名为bruce的用户,该用户在admin数据库上具有dbOwner角色。

Tip

Starting in version 4.2 of the mongo shell, you can use the passwordPrompt() method in conjunction with various user authentication/management methods/commands to prompt for the password instead of specifying the password directly in the method/command call. 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" }
    ]
  }
)