w3resource

MongoDB: db.getRoles() method

db.getRoles()

The db.getRoles() method is used to return information for all the user-defined roles in a database on which the command runs.

Syntax:

db.getRoles()

If run without an argument, db.getRoles() returns inheritance information for the database’s user-defined roles.

Parameters:

Name Description Required /
Optional
Type
rolesInfo Set this field to 1 to retrieve all user-defined roles. Required integer
showPrivileges Set the field to true to show role privileges, including both privileges inherited from other roles and privileges defined directly. By default, the command returns only the roles from which this role inherits privileges and does not return specific privileges. Optional boolean
showBuiltinRoles Set to true to display built-in roles as well as user-defined roles. Optional boolean

Example: MongoDB: db.getRoles() method

The following operations return documents for all the roles on the payroll database, including role privileges and built-in roles:

use payroll
db.getRoles(
    {
      rolesInfo: 1,
      showPrivileges:true,
      showBuiltinRoles: true
    }
)

Output:

> db.getRoles(
...     {
...       rolesInfo: 1,
...       showPrivileges:true,
...       showBuiltinRoles: true
...     }
... );
[
        {
                "role" : "dbAdmin",
                "db" : "payroll",
                "isBuiltin" : true,
                "roles" : [ ],
                "inheritedRoles" : [ ],
                "privileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "collMod",
                                        "collStats",
                                        "compact",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbStats",
                                        "dropCollection",
                                        "dropDatabase",
                                        "dropIndex",
                                        "enableProfiler",
                                        "indexStats",
                                        "planCacheIndexFilter",
                                        "planCacheRead",
                                        "planCacheWrite",
                                        "reIndex",
                                        "renameCollectionSameDB",
                                        "repairDatabase",
                                        "storageDetails",
                                        "validate"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.indexes"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.namespaces"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.profile"
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        }
                ],
                "inheritedPrivileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "collMod",
                                        "collStats",
                                        "compact",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbStats",
                                        "dropCollection",
                                        "dropDatabase",
                                        "dropIndex",
                                        "enableProfiler",
                                        "indexStats",
                                        "planCacheIndexFilter",
                                        "planCacheRead",
                                        "planCacheWrite",
                                        "reIndex",
                                        "renameCollectionSameDB",
                                        "repairDatabase",
                                        "storageDetails",
                                        "validate"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.indexes"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.namespaces"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.profile"
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        }
                ]
        },
        {
                "role" : "dbOwner",
                "db" : "payroll",
                "isBuiltin" : true,
                "roles" : [ ],
                "inheritedRoles" : [ ],
                "privileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "changeCustomData",
                                        "changePassword",
                                        "collMod",
                                        "collStats",
                                        "compact",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "createRole",
                                        "createUser",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "dropDatabase",
                                        "dropIndex",
                                        "dropRole",
                                        "dropUser",
                                        "emptycapped",
                                        "enableProfiler",
                                        "find",
                                        "grantRole",
                                        "indexStats",
                                        "insert",
                                        "killCursors",
                                        "planCacheIndexFilter",
                                        "planCacheRead",
                                        "planCacheWrite",
                                        "reIndex",
                                        "remove",
                                        "renameCollectionSameDB",
                                        "repairDatabase",
                                        "revokeRole",
                                        "storageDetails",
                                        "update",
                                        "validate",
                                        "viewRole",
                                        "viewUser"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.indexes"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.js"
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "dropIndex",
                                        "emptycapped",
                                        "find",
                                        "insert",
                                        "killCursors",
                                        "planCacheRead",
                                        "remove",
                                        "renameCollectionSameDB",
                                        "update"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.namespaces"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.profile"
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        }
                ],
                "inheritedPrivileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "collMod",
                                        "collStats",
                                        "compact",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "dropDatabase",
                                        "dropIndex",
                                        "emptycapped",
                                        "enableProfiler",
                                        "find",
                                        "indexStats",
                                        "insert",
                                        "killCursors",
                                        "planCacheIndexFilter",
                                        "planCacheRead",
                                        "planCacheWrite",
                                        "reIndex",
                                        "remove",
                                        "renameCollectionSameDB",
                                        "repairDatabase",
                                        "storageDetails",
                                        "update",
                                        "validate"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.indexes"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.js"
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "dropIndex",
                                        "emptycapped",
                                        "find",
                                        "insert",
                                        "killCursors",
                                        "planCacheRead",
                                        "remove",
                                        "renameCollectionSameDB",
                                        "update"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.namespaces"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.profile"
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "changeCustomData",
                                        "changePassword",
                                        "createRole",
                                        "createUser",
                                        "dropRole",
                                        "dropUser",
                                        "grantRole",
                                        "revokeRole",
                                        "viewRole",
                                        "viewUser"
                                ]
                        }
                ]
        },
        {
                "role" : "read",
                "db" : "payroll",
                "isBuiltin" : true,
                "roles" : [ ],
                "inheritedRoles" : [ ],
                "privileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.indexes"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.js"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.namespaces"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        }
                ],
                "inheritedPrivileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.indexes"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.js"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.namespaces"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        }
                ]
        },
        {
                "role" : "readWrite",
                "db" : "payroll",
                "isBuiltin" : true,
                "roles" : [ ],
                "inheritedRoles" : [ ],
                "privileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "dropIndex",
                                        "emptycapped",
                                        "find",
                                        "insert",
                                        "killCursors",
                                        "planCacheRead",
                                        "remove",
                                        "renameCollectionSameDB",
                                        "update"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.indexes"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.js"
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "dropIndex",
                                        "emptycapped",
                                        "find",
                                        "insert",
                                        "killCursors",
                                        "planCacheRead",
                                        "remove",
                                        "renameCollectionSameDB",
                                        "update"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.namespaces"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        }
                ],
                "inheritedPrivileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "dropIndex",
                                        "emptycapped",
                                        "find",
                                        "insert",
                                        "killCursors",
                                        "planCacheRead",
                                        "remove",
                                        "renameCollectionSameDB",
                                        "update"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.indexes"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.js"
                                },
                                "actions" : [
                                        "collStats",
                                        "convertToCapped",
                                        "createCollection",
                                        "createIndex",
                                        "dbHash",
                                        "dbStats",
                                        "dropCollection",
                                        "dropIndex",
                                        "emptycapped",
                                        "find",
                                        "insert",
                                        "killCursors",
                                        "planCacheRead",
                                        "remove",
                                        "renameCollectionSameDB",
                                        "update"
                                ]
                        },
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : "system.namespaces"
                                },
                                "actions" : [
                                        "collStats",
                                        "dbHash",
                                        "dbStats",
                                        "find",
                                        "killCursors",
                                        "planCacheRead"
                                ]
                        }
                ]
        },
        {
                "role" : "userAdmin",
                "db" : "payroll",
                "isBuiltin" : true,
                "roles" : [ ],
                "inheritedRoles" : [ ],
                "privileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "changeCustomData",
                                        "changePassword",
                                        "createRole",
                                        "createUser",
                                        "dropRole",
                                        "dropUser",
                                        "grantRole",
                                        "revokeRole",
                                        "viewRole",
                                        "viewUser"
                                ]
                        }
                ],
                "inheritedPrivileges" : [
                        {
                                "resource" : {
                                        "db" : "payroll",
                                        "collection" : ""
                                },
                                "actions" : [
                                        "changeCustomData",
                                        "changePassword",
                                        "createRole",
                                        "createUser",
                                        "dropRole",
                                        "dropUser",
                                        "grantRole",
                                        "revokeRole",
                                        "viewRole",
                                        "viewUser"
                                ]
                        }
                ]
        }
]

Retrieve the restaurants data from here

Previous: db.getRole() method
Next: Queries A simple MongoDB query



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://www.w3resource.com/mongodb/shell-methods/role-management/db-getRoles.php