On this page
grantRolesToUser
在本页面
Definition
grantRolesToUser
- 向用户授予其他角色。
grantRolesToUser命令使用以下语法:
{ grantRolesToUser: "<user>",
roles: [ <roles> ],
writeConcern: { <write concern> }
}
该命令具有以下字段:
Field | Type | Description |
---|---|---|
grantRolesToUser |
string | 赋予其他角色的用户名。 |
roles |
array | 授予用户的一系列其他角色。 |
writeConcern |
document | 可选的。修改的write concern级别。 writeConcern 文档具有与getLastError命令相同的字段。 |
在roles
字段中,您可以同时指定built-in roles和user-defined roles。
要指定运行grantRolesToUser的同一数据库中存在的角色,可以使用该角色的名称指定该角色:
"readWrite"
或者,您可以通过文档指定角色,如:
{ role: "<role>", db: "<database>" }
要指定存在于其他数据库中的角色,请与文档一起指定该角色。
Required Access
您必须在数据库上拥有grantRole action才能在该数据库上授予角色。
Example
在products
数据库中为用户accountUser01
提供以下角色:
"roles" : [
{ "role" : "assetsReader",
"db" : "assets"
}
]
以下grantRolesToUser操作赋予accountUser01
stock
数据库上的read角色和products
数据库上的readWrite角色。
use products
db.runCommand( { grantRolesToUser: "accountUser01",
roles: [
{ role: "read", db: "stock"},
"readWrite"
],
writeConcern: { w: "majority" , wtimeout: 2000 }
} )
products
数据库中的用户accountUser01
现在具有以下角色:
"roles" : [
{ "role" : "assetsReader",
"db" : "assets"
},
{ "role" : "read",
"db" : "stock"
},
{ "role" : "readWrite",
"db" : "products"
}
]