mongodb/node-mongodb-native · error · MongoInvalidArgumentError

Profiling level must be one of "${enumToString(ProfilingLeve

Error message

Profiling level must be one of "${enumToString(ProfilingLevel)}"

What it means

Thrown by SetProfilingLevelOperation.buildCommandDocument when the level string is not one of 'off', 'slow_only', 'all' (the exact values of the ProfilingLevel const). The constructor's switch falls through to profile=0 for unknown levels, and buildCommandDocument then rejects the unknown level with a MongoInvalidArgumentError before issuing the command. Note the values use snake_case ('slow_only'), not 'slow' or camelCase.

Source

Thrown at src/operations/set_profiling_level.ts:61

        break;
      default:
        this.profile = 0;
        break;
    }

    this.level = level;
  }

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

  override buildCommandDocument(_connection: Connection): Document {
    const level = this.level;

    if (!levelValues.has(level)) {
      // TODO(NODE-3483): Determine error to put here
      throw new MongoInvalidArgumentError(
        `Profiling level must be one of "${enumToString(ProfilingLevel)}"`
      );
    }

    return { profile: this.profile };
  }

  override handleOk(
    _response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>
  ): ProfilingLevel {
    return this.level;
  }
}

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Use the exported ProfilingLevel constants: ProfilingLevel.off, ProfilingLevel.slowOnly, ProfilingLevel.all (which resolve to 'off'/'slow_only'/'all').
  2. Pass exactly one of the strings 'off', 'slow_only', 'all'.
  3. Do not pass numbers - the public API takes the string level, not 0/1/2.
  4. Validate the value against the allowed set before calling in dynamic/config-driven code.

Example fix

// before
await db.setProfilingLevel('slow'); // or 1
// after
import { ProfilingLevel } from 'mongodb';
await db.setProfilingLevel(ProfilingLevel.slowOnly); // 'slow_only'
Defensive patterns

Strategy: validation

Validate before calling

import { ProfilingLevel } from 'mongodb';
const allowed = new Set<string>(Object.values(ProfilingLevel)); // 'off','slow_only','all'
if (!allowed.has(level)) throw new Error(`level must be one of ${[...allowed].join(',')}`);
await db.setProfilingLevel(level as any);

Type guard

import { ProfilingLevel } from 'mongodb';
function isProfilingLevel(v: unknown): v is (typeof ProfilingLevel)[keyof typeof ProfilingLevel] {
  return typeof v === 'string' && Object.values(ProfilingLevel).includes(v as any);
}

Prevention

When it happens

Trigger: Calling db.setProfilingLevel('slow'), db.setProfilingLevel('on'), db.setProfilingLevel(1), or any string not in the allowed set. Passing the numeric 0/1/2 instead of the string level. Passing a typo like 'alll' or 'of'.

Common situations: Developers assuming 'slow' or numeric levels work; copy-paste from shell examples that use numbers; confusion between the public string API and the internal numeric profile value.

Related errors


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