On this page本页内容
This is a mongosh
method. This is not the documentation for Node.js
or other programming language specific driver methods.
In most cases, mongosh
methods work the same way as the legacy mongo
shell methods. However, some legacy methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
db.adminCommand(command)
Provides a helper to run specified database commands against the 提供帮助程序,以针对admin
database.admin
数据库运行指定的数据库命令。
command | document or string |
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.有关可用管理数据库命令的列表,请参阅管理命令。
For a 对于使用mongod
or mongos
running with authorization
, the authorized user must have the appropriate privileges to run the database command. authorization
运行的mongod
或mongos
,授权用户必须具有运行数据库命令的适当权限。See the reference documentation for the command for more information on security requirements.有关安全要求的更多信息,请参阅命令的参考文档。
The method returns a response document that contains the following fields:该方法返回包含以下字段的响应文档:
<command result> | command that was run.command 的结果字段。 |
ok | 1 ) or failed (0 ).1 )还是失败(0 )的数字。 |
operationTime |
|
$clusterTime |
|
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 } )
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
管理数据库命令,将测试数据库中的orders
集合重命名为orders-2016
。
db.adminCommand( { renameCollection: "test.orders", to: "test.orders-2016" } )
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
角色。
Starting in version 4.2 of the 从mongoshell 4.2版开始,您可以使用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. 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" } ] } )