{"record":{"id":"75d109b6839d7977","repo":"louislam/uptime-kuma","slug":"mongodb-command-failed","errorCode":null,"errorMessage":"MongoDB command failed","messagePattern":"MongoDB command failed","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/monitor-types/mongodb.js","lineNumber":21,"sourceCode":"const { MongoClient } = require(\"mongodb\");\nconst jsonata = require(\"jsonata\");\n\nclass MongodbMonitorType extends MonitorType {\n    name = \"mongodb\";\n\n    /**\n     * @inheritdoc\n     */\n    async check(monitor, heartbeat, _server) {\n        let command = { ping: 1 };\n        if (monitor.databaseQuery) {\n            command = JSON.parse(monitor.databaseQuery);\n        }\n\n        let result = await this.runMongodbCommand(monitor.databaseConnectionString, command);\n\n        if (result[\"ok\"] !== 1) {\n            throw new Error(\"MongoDB command failed\");\n        } else {\n            heartbeat.msg = \"Command executed successfully\";\n        }\n\n        if (monitor.jsonPath) {\n            let expression = jsonata(monitor.jsonPath);\n            result = await expression.evaluate(result);\n            if (result) {\n                heartbeat.msg = \"Command executed successfully and the jsonata expression produces a result.\";\n            } else {\n                throw new Error(\"Queried value not found.\");\n            }\n        }\n\n        if (monitor.expectedValue) {\n            if (result.toString() === monitor.expectedValue) {\n                heartbeat.msg = \"Command executed successfully and expected value was found\";\n            } else {","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/louislam/uptime-kuma/blob/6b5ea0155793e666666745fb8d6fef1e829543a2/server/monitor-types/mongodb.js#L3-L39","documentation":"Thrown by the MongoDB monitor when the executed command's result has result['ok'] !== 1. The default {ping:1} or a custom monitor.databaseQuery ran against the server but the server reported the command itself as failed. The message is generic (no details), so you must inspect the command and server logs.","triggerScenarios":"runMongodbCommand resolves with an object whose 'ok' field is not 1. E.g. an invalid custom databaseQuery command, a command the user lacks privileges for, or the server in a state (recovering/read-only) that rejects the command.","commonSituations":"databaseQuery JSON is malformed or uses an unsupported command name, the authenticated DB user lacks 'clusterMonitor'/'ping' privileges, or the target is a secondary that rejects writes.","solutions":["Run the command manually against the same connection string to see the server's error","If using a custom databaseQuery, validate it is valid JSON and a supported command","Grant the monitoring user the minimum privileges needed (e.g. ping, serverStatus)","Fall back to the default {ping:1} to isolate whether the connection itself is healthy"],"exampleFix":"// before: custom command the user cannot run\nmonitor.databaseQuery = '{\"replSetGetStatus\": 1}'; // lacks权限\n// after: use a permitted health command\nmonitor.databaseQuery = '{\"ping\": 1}';","handlingStrategy":"validation","validationCode":"// Validate the custom command JSON before connecting\nif (monitor.databaseQuery) {\n    let cmd;\n    try { cmd = JSON.parse(monitor.databaseQuery); } catch {\n        throw new Error(\"databaseQuery is not valid JSON\");\n    }\n    if (!cmd || typeof cmd !== \"object\" || Array.isArray(cmd)) {\n        throw new Error(\"databaseQuery must be a single command object\");\n    }\n}","typeGuard":"/** @param {any} r */\nfunction isOkResult(r) {\n    return r && typeof r === \"object\" && r.ok === 1;\n}","tryCatchPattern":"if (result[\"ok\"] !== 1) {\n    throw new Error(\"MongoDB command failed\");\n}","preventionTips":["Grant the monitoring user the minimum command privileges it needs","Prefer the default {ping:1} unless you have a specific health command","Test custom commands against the same connection string manually first"],"tags":["mongodb","command","permissions","database"],"backgroundTag":null,"analyzedSha":"6b5ea0155793e666666745fb8d6fef1e829543a2","analyzedAt":"2026-08-12T23:42:12.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}