定义
db.revokeRolesFromUser()删除用户在当前数据库中的一个或多个角色。
重要
mongosh 方法
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.有关数据库命令,请参阅
revokeRolesFromUser命令。如需了解 MongoDB API 驱动程序,请参阅特定语言的 MongoDB 驱动程序文档。
The
db.revokeRolesFromUser()method uses the following syntax:db.revokeRolesFromUser( "<username>", [ <roles> ], { <writeConcern> } ) The
db.revokeRolesFromUser()method takes the following arguments:To specify a role that exists in the same database where
db.revokeRolesFromUser()runs, you can either specify the role with the name of the role:"readWrite" 或者,可以使用文档指定角色,如下所示:
{ role: "<role>", db: "<database>" } 要指定存在于其他数据库中的角色,请使用文档指定该角色。
The
db.revokeRolesFromUser()method wraps therevokeRolesFromUsercommand.
兼容性
此方法可用于以下环境中托管的部署:
重要
MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令。
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
行为
副本集
If run on a replica set, db.revokeRolesFromUser() is executed using "majority" write concern by default.
必需的访问权限
必须对数据库执行 revokeRole 动作才能撤消该数据库上的角色。
例子
products数据库中的accountUser01用户具有以下角色:
"roles" : [ { "role" : "assetsReader", "db" : "assets" }, { "role" : "read", "db" : "stock" }, { "role" : "readWrite", "db" : "products" } ]
The following db.revokeRolesFromUser() method removes the two of the user's roles: the read role on the stock database and the readWrite role on the products database, which is also the database on which the method runs:
use products db.revokeRolesFromUser( "accountUser01", [ { role: "read", db: "stock" }, "readWrite" ], { w: "majority" } )
products数据库中的用户accountUser01用户现在只剩下一个角色:
"roles" : [ { "role" : "assetsReader", "db" : "assets" } ]