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
- Use only: command, topology, serverSelection, connection, client, or default.
- Remember keys are the enum VALUES (lowercase camelCase), not the TypeScript constant names.
- 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
- Use enum VALUES (command, topology, serverSelection, connection, client), not the constant NAMES.
- Use `default` as the catch-all.
- Build the allowed-keys list from Object.values(MongoLoggableComponent).
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
- Option 'mongodbLogComponentSeverities' does not support
- Option 'mongodbLogComponentSeverities' must be a non-null…
- Option 'mongodbLogPath' must be of type 'stderr' | 'stdout'…
- User input for option 'mongodbLogComponentSeverities'…
- Auth mechanism property ALLOWED_HOSTS must be an array of…
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)