louislam/uptime-kuma · error · Error

MongoDB command failed

Error message

MongoDB command failed

What it means

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.

Source

Thrown at server/monitor-types/mongodb.js:21

const { MongoClient } = require("mongodb");
const jsonata = require("jsonata");

class MongodbMonitorType extends MonitorType {
    name = "mongodb";

    /**
     * @inheritdoc
     */
    async check(monitor, heartbeat, _server) {
        let command = { ping: 1 };
        if (monitor.databaseQuery) {
            command = JSON.parse(monitor.databaseQuery);
        }

        let result = await this.runMongodbCommand(monitor.databaseConnectionString, command);

        if (result["ok"] !== 1) {
            throw new Error("MongoDB command failed");
        } else {
            heartbeat.msg = "Command executed successfully";
        }

        if (monitor.jsonPath) {
            let expression = jsonata(monitor.jsonPath);
            result = await expression.evaluate(result);
            if (result) {
                heartbeat.msg = "Command executed successfully and the jsonata expression produces a result.";
            } else {
                throw new Error("Queried value not found.");
            }
        }

        if (monitor.expectedValue) {
            if (result.toString() === monitor.expectedValue) {
                heartbeat.msg = "Command executed successfully and expected value was found";
            } else {

View on GitHub (pinned to 6b5ea01557)

Solutions

  1. Run the command manually against the same connection string to see the server's error
  2. If using a custom databaseQuery, validate it is valid JSON and a supported command
  3. Grant the monitoring user the minimum privileges needed (e.g. ping, serverStatus)
  4. Fall back to the default {ping:1} to isolate whether the connection itself is healthy

Example fix

// before: custom command the user cannot run
monitor.databaseQuery = '{"replSetGetStatus": 1}'; // lacks权限
// after: use a permitted health command
monitor.databaseQuery = '{"ping": 1}';
Defensive patterns

Strategy: validation

Validate before calling

// Validate the custom command JSON before connecting
if (monitor.databaseQuery) {
    let cmd;
    try { cmd = JSON.parse(monitor.databaseQuery); } catch {
        throw new Error("databaseQuery is not valid JSON");
    }
    if (!cmd || typeof cmd !== "object" || Array.isArray(cmd)) {
        throw new Error("databaseQuery must be a single command object");
    }
}

Type guard

/** @param {any} r */
function isOkResult(r) {
    return r && typeof r === "object" && r.ok === 1;
}

Try / catch

if (result["ok"] !== 1) {
    throw new Error("MongoDB command failed");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of louislam/uptime-kuma@6b5ea01557 (2026-08-12). Data as JSON: /api/errors/75d109b6839d7977. Report an issue: GitHub.