对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

db.getRole()(mongosh方法)

db.getRole(rolename, args)

返回此角色从中继承权限的角色。该方法还可以选择返回角色的所有权限。

Run db.getRole() from the database that contains the role. The command can retrieve information for both user-defined roles and built-in roles.

The db.getRole() method accepts the following parameters:

Parameter
类型
说明

rolename

字符串

角色的名称。

args

文档

可选。指定附加参数的文档。

args 文档支持以下可选字段:

字段
类型
说明

showAuthenticationRestrictions

布尔

可选。将该字段设置为 true,以在输出中包含身份验证限制。身份验证限制指出了具有此角色的用户可以连接的 IP 地址。

默认情况下,此字段为 false,这意味着 db.getRole() 输出不包含身份验证限制。

showBuiltinRoles

布尔

可选。将此字段设置为 true 以在输出中包含内置角色。默认情况下,该字段设置为 falserolesInfo: 1 的输出只显示用户定义的角色。

showPrivileges

布尔

可选。将此字段设置为 true 以显示角色权限,包括从其他角色继承的权限和直接定义的权限。默认情况下,该命令仅返回此角色从中继承权限的角色,而不返回特定权限。

db.getRole() wraps the rolesInfo command.

此方法可用于以下环境中托管的部署:

重要

MongoDB Atlas 集群不支持此命令。有关 Atlas 对所有命令的支持的信息,请参阅不支持的命令

要查看角色的信息,您必须已被显式授予该角色,或必须有权对该角色的数据库执行 viewRole 操作

本节中的示例展示如何使用 db.getRoles 来:

以下操作返回 products 数据库上定义的角色 associate 的角色继承信息:

use products
db.getRole( "associate" )

示例输出:

{
_id: 'products.associate',
role: 'associate',
db: 'products',
roles: [ { role: 'readWrite', db: 'products' } ],
inheritedRoles: [ { role: 'readWrite', db: 'products' } ],
isBuiltin: false
}

以下操作返回 products 数据库上定义的角色 associate 的角色继承信息和权限

use products
db.getRole( "associate", { showPrivileges: true } )

示例输出:

{
_id: 'products.associate',
role: 'associate',
db: 'products',
privileges: [
{
resource: { db: 'products', collection: '' },
actions: [ 'bypassDocumentValidation' ]
}
],
roles: [ { role: 'readWrite', db: 'products' } ],
inheritedRoles: [ { role: 'readWrite', db: 'products' } ],
inheritedPrivileges: [
{
resource: { db: 'products', collection: '' },
actions: [ 'bypassDocumentValidation' ]
},
{
resource: { db: 'products', collection: '' },
actions: [
'changeStream',
'collStats',
'compactStructuredEncryptionData',
'convertToCapped',
'createCollection',
'createIndex',
'dbHash',
'dbStats',
'dropCollection',
'dropIndex',
'find',
'insert',
'killCursors',
'listCollections',
'listIndexes',
'planCacheRead',
'remove',
'renameCollectionSameDB',
'update'
]
}
],
isBuiltin: false
}

以下操作返回 products 数据库上定义的角色 associate 的继承信息和身份验证限制:

use products
db.getRole( "associate", { showAuthenticationRestrictions: true } )

示例输出:

{
_id: 'products.associate',
role: 'associate',
db: 'products',
roles: [ { role: 'readWrite', db: 'products' } ],
authenticationRestrictions: [
[ { clientSource: [ '198.51.100.0' ] } ]
],
inheritedRoles: [ { role: 'readWrite', db: 'products' } ],
inheritedAuthenticationRestrictions: [
[ { clientSource: [ '198.51.100.0' ] } ]
],
isBuiltin: false
}