定义
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
注意
This command is supported in all MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.runCommand( { rolesInfo: { role: <name>, db: <db> }, showAuthenticationRestrictions: <Boolean>, showBuiltinRoles: <Boolean>, showPrivileges: <Boolean>, comment: <any> } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
| 字符串、文档、数组或整数 | The role(s) to return information about. For the syntax for specifying roles, see Behavior. |
| 布尔 | 可选。将该字段设置为 默认情况下,此字段为 |
| 布尔 | |
| 布尔 | 可选。 将该字段设置为 |
| any | 可选。用户提供的待附加到该命令的注释。设置后,该注释将与该命令的记录一起出现在以下位置:
注释可以是任何有效的 BSON 类型(字符串、整型、对象、数组等)。 |
行为
返回单个角色的信息
要指定当前数据库中的角色,请按角色名称指定:
{ rolesInfo: "<rolename>" }
要指定其他数据库中的角色,请通过指定角色和数据库的文档来指定角色:
{ rolesInfo: { role: "<rolename>", db: "<database>" } }
返回多个角色的信息
要指定多个角色,请使用数组。 将数组中的每个角色指定为文档或string 。 仅当运行命令的数据库上存在该角色时才使用string :
{ rolesInfo: [ "<rolename>", { role: "<rolename>", db: "<database>" }, ... ] }
返回数据库中所有角色的信息
要指定运行该命令的数据库中的所有角色,请指定rolesInfo: 1 。 默认情况下,MongoDB 会显示数据库中所有用户定义的角色。 要同时包含内置角色,请包含参数值对showBuiltinRoles: true :
{ rolesInfo: 1, showBuiltinRoles: true }
必需的访问权限
输出
rolesInfo.inheritedRolesAll roles from which this role inherits privileges. This includes the roles in the
rolesInfo.rolesarray as well as the roles from which the roles in therolesInfo.rolesarray inherit privileges. All privileges apply to the current role. The documents in this field list the roles and the databases on which they are defined.
rolesInfo.privilegesThe privileges directly specified by this role; i.e. the array excludes privileges inherited from other roles. By default the output does not include the
privilegesfield. To include the field, specifyshowPrivileges: truewhen running therolesInfocommand.
rolesInfo.inheritedPrivilegesAll privileges granted by this role, including those inherited from other roles. By default the output does not include the
inheritedPrivilegesfield. To include the field, specifyshowPrivileges: truewhen running therolesInfocommand.
示例
本节中的示例展示如何使用rolesInfo命令:
查看单个角色的信息
以下命令返回products数据库中定义的角色associate的角色继承信息:
db.runCommand( { rolesInfo: { role: "associate", db: "products" } } )
以下命令返回运行该命令的数据库上角色siteManager的角色继承信息:
db.runCommand( { rolesInfo: "siteManager" } )
以下命令返回products数据库上定义的角色associate的角色继承和特权:
db.runCommand( { rolesInfo: { role: "associate", db: "products" }, showPrivileges: true } )
查看多个角色的信息
以下命令将返回两个不同数据库上两个角色的信息:
db.runCommand( { rolesInfo: [ { role: "associate", db: "products" }, { role: "manager", db: "resources" } ] } )
以下同时返回角色继承和特权:
db.runCommand( { rolesInfo: [ { role: "associate", db: "products" }, { role: "manager", db: "resources" } ], showPrivileges: true } )
查看数据库的所有用户定义角色
以下操作返回运行该命令的数据库上的所有用户定义角色,并包括特权:
db.runCommand( { rolesInfo: 1, showPrivileges: true } )
示例输出(为便于阅读已被缩短):
{ roles: [ { _id: 'products.associate', role: 'associate', db: 'products', privileges: [ { resource: { db: 'products', collection: '' }, actions: [ 'bypassDocumentValidation' ] } ], roles: [ { role: 'readWrite', db: 'products' } ], isBuiltin: false, inheritedRoles: [ { role: 'readWrite', db: 'products' } ], inheritedPrivileges: [ { resource: { db: 'products', collection: '' }, actions: [ 'bypassDocumentValidation' ] }, { resource: { db: 'products', collection: '' }, actions: [ 'changeStream', 'collStats', 'compactStructuredEncryptionData', ... ] }, ... ] } ], ok: 1 }
查看数据库的所有用户定义角色和内置角色
以下操作返回运行该命令的数据库上的所有角色,包括内置角色和用户定义的角色:
db.runCommand( { rolesInfo: 1, showBuiltinRoles: true } )
示例输出(为便于阅读已被缩短):
{ roles: [ { role: 'enableSharding', db: 'products', isBuiltin: true, roles: [], inheritedRoles: [] }, { role: 'userAdmin', db: 'products', isBuiltin: true, roles: [], inheritedRoles: [] }, { role: 'read', db: 'products', isBuiltin: true, roles: [], inheritedRoles: [] }, ... ], ok: 1 }
查看角色的身份验证限制
以下操作返回products数据库上的所有用户定义角色,并包括身份验证限制:
db.runCommand( { rolesInfo: 1, showAuthenticationRestrictions: true } )
示例输出:
{ roles: [ { _id: 'products.associate', role: 'associate', db: 'products', roles: [ { role: 'readWrite', db: 'products' } ], authenticationRestrictions: [ [ { clientSource: [ '198.51.100.0' ] } ] ], isBuiltin: false, inheritedRoles: [ { role: 'readWrite', db: 'products' } ], inheritedAuthenticationRestrictions: [ [ { clientSource: [ '198.51.100.0' ] } ] ] } ], ok: 1 }