13: Unauthorized (error code 13)
MongoDBWhat does it mean?
The authenticated user has no privilege for the attempted operation or resource. The user is logged in, but their roles do not cover this action.
Why does it happen?
The required role (e.g. readWrite, dbAdmin) is not granted to the user, the operation runs against the wrong database, or privileges are insufficient for an administrative command. Incomplete RBAC configuration.
How to fix it
Check the user's roles: db.getUser("app_user"). Grant the required role: db.grantRolesToUser("app_user", [{role:"readWrite", db:"mydb"}]). Make sure the operation runs in the correct database. Assign an appropriate built-in or custom role following the principle of least privilege. Verify the authSource value in the connection string.