{"record":{"id":"36f1c3e43af460af","repo":"1Panel-dev/1Panel","slug":"mongodb-user-not-found-username","errorCode":null,"errorMessage":"mongodb user not found: ${userName}","messagePattern":"mongodb user not found: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"agent/app/service/database_mongodb.go","lineNumber":795,"sourceCode":"\tpermissionJSON, err := json.Marshal(permission)\n\tif err != nil {\n\t\treturn err\n\t}\n\tscript := strings.TrimSpace(fmt.Sprintf(`\nconst dbName = %s;\nconst userName = %s;\nconst permission = %s;\nconst targetDb = db.getSiblingDB(dbName);\nconst userInfo = targetDb.runCommand({\n  usersInfo: userName,\n  showCredentials: false,\n  showCustomData: false\n});\nif (!userInfo || userInfo.ok !== 1) {\n  throw new Error(\"failed to load mongodb user privileges\");\n}\nif (!Array.isArray(userInfo.users) || userInfo.users.length === 0) {\n  throw new Error(\"mongodb user not found: \" + userName);\n}\nconst roles = (userInfo.users[0].roles || []).filter(role => role.db !== dbName);\nroles.push({ role: permission, db: dbName });\nconst result = targetDb.runCommand({\n  updateUser: userName,\n  roles: roles\n});\nif (!result || result.ok !== 1) {\n  throw new Error(\"failed to update mongodb user privileges\");\n}\n`, databaseJSON, usernameJSON, permissionJSON))\n\treturn runMongodbAdminScript(connectionName, script)\n}\n\nfunc loadRemoteMongodbPrivilege(connectionName, dbName, username string) (string, error) {\n\tinfo, err := loadRemoteMongodbConnection(connectionName)\n\tif err != nil {\n\t\treturn \"\", err","sourceCodeStart":777,"sourceCodeEnd":813,"githubUrl":"https://github.com/1Panel-dev/1Panel/blob/5ac7c808815b0691009cd390414f03471312262f/agent/app/service/database_mongodb.go#L777-L813","documentation":"The privilege-change script found the user lookup succeeded (ok:1) but `userInfo.users` was empty — the named user does not exist on the database the script is looking at. Unlike errors 7/8 this is a clean negative lookup: usersInfo legitimately returns zero users when the user lives on another db (typically `admin`) or was deleted.","triggerScenarios":"Changing permissions for a user created on the `admin` authenticationDatabase while the script queries the workload db; the user was deleted between the UI listing and the submit; typo/case mismatch in username (MongoDB usernames are case-sensitive).","commonSituations":"Users provisioned by external tooling on admin; stale UI list after deletion; mixed-case usernames copied from docs.","solutions":["Confirm where the user lives: `db.getSiblingDB(\"admin\").runCommand({usersInfo:{user:\"<u>\",db:\"<db>\"}})`","If deleted, re-create via the bind flow and retry the permission change","If it lives on admin, either recreate it on the target db or adjust the record so the script targets the owning db","Check exact username spelling/case against the UI list"],"exampleFix":"null","handlingStrategy":"type-guard","validationCode":"// resolve the user's home db before the change\n// db.getSiblingDB(\"admin\").runCommand({usersInfo:{user:u, db:dbName}}).users.length","typeGuard":"// Go: guard that the user exists on the target db before building the script\nfunc mongodbUserExists(database, dbName, username string) bool {\n    script := fmt.Sprintf(`print((db.getSiblingDB(%s).runCommand({usersInfo:%s}).users||[]).length)`, dbName, username)\n    out, err := runMongodbAdminScriptWithStdout(database, script)\n    return err == nil && strings.TrimSpace(out) == \"1\"\n}","tryCatchPattern":"if err := changePrivilege(...); err != nil {\n    if strings.Contains(err.Error(), \"mongodb user not found\") {\n        // offer to re-create the user on this db, or re-point the record\n    }\n}","preventionTips":["Treat usernames as case-sensitive identifiers; copy-paste them","After deleting a user, refresh the UI list before further edits"],"tags":["mongodb","user-not-found","usersinfo"],"backgroundTag":null,"analyzedSha":"5ac7c808815b0691009cd390414f03471312262f","analyzedAt":"2026-08-15T14:02:06.953Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}