Error
Error Code: 31

MongoDB Error 31: Role Not Found

📦 MongoDB
📋

Description

Error 31, 'Role Not Found', indicates that a specified role name does not exist within the current MongoDB deployment or database context. This typically occurs during user creation, role assignment, or when attempting to grant privileges to a non-existent role.
💬

Error Message

Role Not Found
🔍

Known Causes

3 known causes
⚠️
Role Not Defined
The specified role has not been created or defined in the MongoDB database where it is being referenced.
⚠️
Typographical Error in Role Name
The role name provided contains a typo, capitalization mismatch, or incorrect characters, leading MongoDB to believe it doesn't exist.
⚠️
Incorrect Database Context
The role is being referenced from a database where it was not defined, as roles are scoped to the database they are created in (except for global roles).
🛠️

Solutions

4 solutions available

1. Verify Role Name and Case Sensitivity easy

Ensures the role name is spelled correctly and matches the case of the existing role.

1
Connect to your MongoDB instance using the `mongosh` shell or `mongo` shell.
mongosh
2
Switch to the database where the role is defined (or the admin database if it's a global role).
use admin
3
List all available roles to identify the correct name and case.
db.getRoles({showBuiltinRoles: false})
4
Carefully re-enter the role name in your command or application configuration, ensuring it exactly matches the output from the previous step. Pay close attention to capitalization.
Example: If the role is listed as 'readWriteAnyDatabase', ensure you are using 'readWriteAnyDatabase' and not 'ReadWriteAnyDatabase' or 'readwriteanydatabase'.

2. Create or Re-create the Missing Role medium

If the role truly doesn't exist, this solution creates it with the necessary privileges.

1
Connect to your MongoDB instance using `mongosh`.
mongosh
2
Switch to the database where the role should reside. If it's a custom role intended for a specific database, use that database. For global roles, use the `admin` database.
use yourDatabaseName
3
Define the role with the desired name and privileges. Replace 'yourRoleName' and the privileges with your specific requirements.
db.createRole({
  role: "yourRoleName",
  privileges: [
    { resource: { anyResource: true }, actions: [ "find", "read" ] }
  ],
  roles: []
})
4
If the role was previously dropped or corrupted, you might need to re-apply its definition. The `createRole` command will fail if the role already exists, so you might consider dropping it first if you are certain you want to redefine it.
db.dropRole("yourRoleName")
db.createRole({
  role: "yourRoleName",
  privileges: [
    { resource: { anyResource: true }, actions: [ "find", "read" ] }
  ],
  roles: []
})

3. Grant the Role to a User medium

Ensures the user attempting to use the role has been explicitly granted it.

1
Connect to your MongoDB instance using `mongosh`.
mongosh
2
Switch to the database where the user is defined (or the `admin` database for users defined in `admin`).
use admin
3
Grant the role to the specific user. Replace 'yourUsername', 'yourDatabaseName', and 'yourRoleName' with your actual values.
db.grantRolesToUser("yourUsername", [ { role: "yourRoleName", db: "yourDatabaseName" } ])
4
If the role is a built-in role (e.g., 'readWriteAnyDatabase'), the `db` field might be omitted or specified as 'admin' depending on the role's scope.
Example for a built-in role:
db.grantRolesToUser("yourUsername", [ { role: "readWriteAnyDatabase", db: "admin" } ])

4. Check Role Scope and Database Context medium

Verifies that the role is being accessed within the correct database context.

1
When creating or granting roles, ensure the `db` field in the role definition or grant statement correctly specifies the database the role applies to.
For a role defined in 'myDatabase':
db.createRole({
  role: "myCustomRole",
  privileges: [...],
  roles: [],
  db: "myDatabase"
})
2
When granting a role, ensure you specify the correct database for the role.
db.grantRolesToUser("myUser", [ { role: "myCustomRole", db: "myDatabase" } ])
3
When a user attempts to perform an action, they must be authenticated to a database that has access to the role, or the role must be defined in the `admin` database and have a broad enough scope.
Example of connecting to a specific database:
mongosh "mongodb://user:password@host:port/myDatabase?authSource=admin"
🔗

Related Errors

5 related errors