w3resource

MongoDB: db.grantRolesToRole() method

db.grantRolesToRole()

The db.grantRolesToRole() method is used to specify roles from which a user-defined role inherits privileges.

Syntax:

db.grantRolesToRole( "<rolename>", [ <roles> ], { <writeConcern> } )

Parameters:

Name Description Required /
Optional
Type
rolename The name of the role to which to grant sub roles. Required String
roles An array of roles from which to inherit. Required array
writeConcern The level of write concern for the modification. The writeConcern document takes the same fields as the getLastError command. Optional document

In the roles field, you can specify both built-in roles and user-defined role.

To specify a role that exists in the same database where db.grantRolesToRole() runs, you can either specify the role with the name of the role:

"readWrite"

Or you can specify the role with a document, as in:

{ role: "<role>", db: "<database>" }

To specify a role that exists in a different database, specify the role with a document.

Example: MongoDB: db.grantRolesToRole() method

The following grantRolesToRole() operation updates the admin myroll1  role in the admin database to inherit the privileges of admin read role:

use admin
db.grantRolesToRole(
    "myroll1",
    [ "read" ],
    { w: "majority" , wtimeout: 5000 }
);

Retrieve the restaurants data from here

Behavior

A role can inherit privileges from other roles in its database. A role created on the admin database can inherit privileges from roles in any database.

Previous: db.revokeRolesFromRole() method
Next: db.getRole() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/shell-methods/role-management/db-grantRolesToRole.php