mongodb/node-mongodb-native · error · MongoUnexpectedServerResponseError

Illegal profiling level value

Error message

Illegal profiling level value ${was}

What it means

Thrown as a MongoUnexpectedServerResponseError by ProfilingLevelOperation.handleOk when the server's 'profile' response reports ok=1 but the 'was' field is not 0, 1, or 2. Those three integers map to off/slow_only/all; any other value is an illegal profiling level that the driver cannot interpret, indicating an unexpected server response (e.g. a future or non-standard server build).

Solutions

  1. Verify you are connecting to a genuine MongoDB server of a supported version.
  2. Check for any proxy/middleware mangling the profile command response.
  3. Report the unexpected 'was' value to the driver maintainers with the server version.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  const level = await db.profilingLevel();
} catch (err) {
  if (err instanceof MongoUnexpectedServerResponseError && /Illegal profiling level value/.test(err.message)) {
    // server returned an unrecognized 'was' value; log it and verify server build
  } else throw err;
}

Prevention

When it happens

Trigger: Calling db.setProfilingLevel / db.profilingLevel and the server returns a 'was' value outside {0,1,2}. The mapping at profiling_level.ts:37-41 falls through to the throw at line 41.

Common situations: Connecting to a non-standard, forked, or future MongoDB build that defines extra profiling levels; corrupted/parsed response from a proxy that altered the field; middleware that rewrites command responses.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/1a69910da1fa9c6b. Report an issue: GitHub.

Appendix: source

Thrown at src/operations/profiling_level.ts:41

    super(db, options);
    this.options = options;
  }

  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 dce7939f86)