mongodb/node-mongodb-native · error · MongoAPIError

User input for option 'mongodbLogComponentSeverities'…

Error message

User input for option 'mongodbLogComponentSeverities' contains invalid key: ${k}

What it means

Each key in `mongodbLogComponentSeverities` must be a recognized component. At src/connection_string.ts:1265 the driver checks `Object.values(MongoLoggableComponent).some(val => val === k) || k === 'default'`. The valid components (frozen at src/mongo_logger.ts:139) are `command`, `topology`, `serverSelection`, `connection`, `client`, plus the special `default`. Any other key throws MongoAPIError at src/connection_string.ts:1266.

Solutions

  1. Use only: command, topology, serverSelection, connection, client, or default.
  2. Remember keys are the enum VALUES (lowercase camelCase), not the TypeScript constant names.
  3. Use `default` as the catch-all key.

Example fix

// before
new MongoClient(uri, { mongodbLogComponentSeverities: { query: 'debug' } });

// after
new MongoClient(uri, { mongodbLogComponentSeverities: { command: 'debug' } });
Defensive patterns

Strategy: validation

Validate before calling

import { MongoLoggableComponent } from 'mongodb';
const KEYS = [...Object.values(MongoLoggableComponent), 'default'];
if (opts.mongodbLogComponentSeverities) {
  for (const k of Object.keys(opts.mongodbLogComponentSeverities)) {
    if (!KEYS.includes(k)) throw new Error(`invalid component key: ${k}`);
  }
}

Type guard

function isComponentKey(v: string): boolean { return [...Object.values(MongoLoggableComponent), 'default'].includes(v as any); }

Try / catch

try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoAPIError && /invalid key/.test(e.message)) { /* drop unknown keys, retry */ } else throw e; }

Prevention

When it happens

Trigger: Passing `{ query: 'debug' }`, `{ server: 'info' }`, `{ replication: 'warn' }`, or `{ Command: 'debug' }` (case mismatch).

Common situations: Inventing component names from server-side logging vocab; using the enum's TypeScript identifier (e.g. `COMMAND`) instead of its value (`command`); typos.

Related errors


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

Appendix: source

Thrown at src/connection_string.ts:1266

          `Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable`
        );
      }
      return value;
    }
  },
  mongodbLogComponentSeverities: {
    transform({ values: [value] }) {
      if (typeof value !== 'object' || !value) {
        throw new MongoAPIError(`Option 'mongodbLogComponentSeverities' must be a non-null object`);
      }
      for (const [k, v] of Object.entries(value)) {
        if (typeof v !== 'string' || typeof k !== 'string') {
          throw new MongoAPIError(
            `User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value`
          );
        }
        if (!Object.values(MongoLoggableComponent).some(val => val === k) && k !== 'default') {
          throw new MongoAPIError(
            `User input for option 'mongodbLogComponentSeverities' contains invalid key: ${k}`
          );
        }
        if (!Object.values(SeverityLevel).some(val => val === v)) {
          throw new MongoAPIError(
            `Option 'mongodbLogComponentSeverities' does not support ${v} as a value for ${k}`
          );
        }
      }
      return value;
    }
  },
  mongodbLogMaxDocumentLength: { type: 'uint' },
  // Custom types for modifying core behavior
  connectionType: { type: 'any' },
  srvPoller: { type: 'any' },
  // Accepted Node.js Options
  allowPartialTrustChain: { type: 'any' },

View on GitHub (pinned to dce7939f86)