mongodb/node-mongodb-native · error · MongoUnexpectedServerResponseError

Error with profile command

Error message

Error with profile command

What it means

Thrown by ProfilingLevelOperation.handleOk when the server's profile command returns ok !== 1, i.e., the command itself failed but the response was still parsed as a success envelope. A MongoUnexpectedServerResponseError - the driver expected a successful profile result and got a failure-status document. Common upstream causes are insufficient privileges or running the command against a database/context where profiling is not allowed.

Source

Thrown at src/operations/profiling_level.ts:43

  }

  override get commandName() {
    return 'profile' as const;
  }

  override buildCommandDocument(_connection: Connection): Document {
    return { profile: -1 };
  }

  override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): string {
    if (response.ok === 1) {
      const was = response.was;
      if (was === 0) return 'off';
      if (was === 1) return 'slow_only';
      if (was === 2) return 'all';
      throw new MongoUnexpectedServerResponseError(`Illegal profiling level value ${was}`);
    } else {
      throw new MongoUnexpectedServerResponseError('Error with profile command');
    }
  }
}

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Run db.runCommand({ profile: -1 }) directly to surface the server's actual error code/message (often a permissions failure).
  2. Connect with a user that has the clusterMonitor or root/admin role on the admin database.
  3. Confirm profiling is supported on your deployment (some managed tiers disable it).
  4. Catch MongoUnexpectedServerResponseError and degrade gracefully in monitoring tooling that doesn't strictly need the level.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const level = await db.getProfilingLevel();
} catch (e) {
  if (e instanceof MongoUnexpectedServerResponseError && /profile command/.test(e.message)) {
    // run db.runCommand({ profile: -1 }) to see the real server error (often permissions)
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling db.getProfilingLevel() as a non-privileged user (profiling requires the clusterMonitor or equivalent role / admin access); running it against a serverless or Atlas tier where profiling is restricted; the profile command being unavailable on the target deployment type.

Common situations: Connecting with an application read/write user instead of an admin/monitoring user; serverless MongoDB restrictions; managed-service permission scopes that block the profile command.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/651c7baa14823e18.json. Report an issue: GitHub.