mongodb/node-mongodb-native · error · MongoInvalidArgumentError

Profiling level must be one of

Error message

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

What it means

Thrown as a MongoInvalidArgumentError by SetProfilingLevelOperation.buildCommandDocument when the level string is not one of 'off', 'slow_only', or 'all' (the levelValues set). The valid values come from the ProfilingLevel const object; the message lists them via enumToString. The constructor's switch defaults unknown levels to profile 0, but buildCommandDocument re-validates and rejects before sending.

Solutions

  1. Use the exported ProfilingLevel constants: db.setProfilingLevel(ProfilingLevel.slowOnly).
  2. Pass exactly one of the strings 'off', 'slow_only', or 'all'.
  3. Normalize configuration input to one of the three valid strings before calling.

Example fix

// before
await db.setProfilingLevel('slow'); // typo -> error

// after
const { ProfilingLevel } = require('mongodb');
await db.setProfilingLevel(ProfilingLevel.slowOnly); // 'slow_only'
Defensive patterns

Strategy: validation

Validate before calling

import { ProfilingLevel } from 'mongodb';

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

Type guard

function isProfilingLevel(v: unknown): v is 'off' | 'slow_only' | 'all' {
  return v === 'off' || v === 'slow_only' || v === 'all';
}

Prevention

When it happens

Trigger: Calling db.setProfilingLevel('off') with a typo or unsupported value, e.g. db.setProfilingLevel('slow'), db.setProfilingLevel('none'), or db.setProfilingLevel(1) (number instead of string). The check is `!levelValues.has(level)` at set_profiling_level.ts:59.

Common situations: Using legacy/abbreviated level names from older APIs; passing a numeric level directly; passing an untyped value from configuration that is neither off/slow_only/all.

Related errors


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

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