重要
mongosh 方法
This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.
如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
定义
db.adminCommand(command)提供针对
admin数据库运行指定数据库命令的辅助程序。
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
注意
所有 MongoDB Atlas 集群都支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
行为
db.adminCommand() runs commands against the admin database regardless of the database context in which it runs. The following commands are equivalent:
db.getSiblingDB("admin").runCommand(<command>) db.adminCommand(<command>)
有关可用管理数据库命令的列表,请参阅数据库命令。
注意
对于使用 authorization 运行的 mongod 或 mongos,经过授权的用户必须拥有适当的权限才能运行此数据库命令。有关安全要求的更多信息,请参阅此命令的参考文档。
响应
该方法返回包含以下字段的响应文档:
字段 | 说明 |
|---|---|
<command result> | 特定于运行的 |
| 表明命令成功 ( |
| 操作的逻辑时间。MongoDB 使用逻辑时间对排序操作。仅适用于副本集和分片集群。 如果命令不生成 oplog 条目,例如读操作,则该操作不会推进逻辑时钟。在这种情况下,
对于与因果一致会话相关的操作,MongoDB 驱动程序使用逻辑时间自动设置读取操作和 |
| 返回已签名集群时间的文档。集群时间是用于操作排序的逻辑时间。仅适用于副本集和分片集群。仅供内部使用。 该文档包含以下字段:
|
示例
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.
提示
您可以将 passwordPrompt() 方法与各种用户身份验证/管理方法/命令结合使用,以提示输入密码,而不是直接在方法/命令调用中指定密码。不过,您仍然可以像使用早期版本的 mongo shell 一样直接指定密码。
db.adminCommand( { createUser: "bruce", pwd: passwordPrompt(), // or <cleartext password> roles: [ { role: "dbOwner", db: "admin" } ] } )