mongodb/node-mongodb-native · error · MongoParseError
Invalid WriteConcern cannot parse
Error message
Invalid WriteConcern cannot parse: ${JSON.stringify(value)} What it means
The `writeConcern` option transform at src/connection_string.ts:1185 accepts four shapes: an object/WriteConcern instance (record check), the literal string `'majority'`, or a number (used as `w`). Any other value falls through to MongoParseError at src/connection_string.ts:1202.
Solutions
- Pass an object: `writeConcern: { w: 'majority', j: true }`.
- Or a numeric w: `writeConcern: 1` (number, not string).
- Or the literal string: `writeConcern: 'majority'`.
Example fix
// before
new MongoClient(uri, { writeConcern: 'all' });
// after
new MongoClient(uri, { writeConcern: { w: 'majority' } }); Defensive patterns
Strategy: type-guard
Validate before calling
import { WriteConcern } from 'mongodb';
function isWriteConcernInput(v: unknown): boolean {
return v instanceof WriteConcern || (typeof v === 'object' && v !== null) || v === 'majority' || typeof v === 'number';
}
if (opts.writeConcern != null && !isWriteConcernInput(opts.writeConcern)) {
throw new Error('writeConcern must be an object, WriteConcern, "majority", or a number');
} Type guard
function isWriteConcernInput(v: unknown): v is object | 'majority' | number { return (typeof v === 'object' && v !== null) || v === 'majority' || typeof v === 'number'; } Try / catch
try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoParseError && /WriteConcern cannot parse/.test(e.message)) { opts.writeConcern = { w: 'majority' }; client = new MongoClient(uri, opts); } else throw e; } Prevention
- Always pass writeConcern as an object `{ w, j, wtimeoutMS }`.
- Use numeric `w` values as numbers, not strings.
- Type-check values decoded from JSON before forwarding.
When it happens
Trigger: Passing `writeConcern: 'all'`, `writeConcern: true`, `writeConcern: ['majority']`, `writeConcern: '1'` (string number, not number), or `writeConcern: null`.
Common situations: Confusing with `w: 'majority'` syntax; passing a JSON-decoded value that became a string; treating writeConcern as a boolean 'enable safe writes' flag.
Related errors
- Cannot make WriteConcern from wtimeout
- compressors must be an array or a comma-delimited list of…
- Option 'mongodbLogComponentSeverities' must be a non-null…
- Option 'mongodbLogPath' must be of type 'stderr' | 'stdout'…
- Option pkFactory must be an object with a createPk…
AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11).
Data as JSON: /api/errors/6f113727cc425381.
Report an issue: GitHub.
Appendix: source
Thrown at src/connection_string.ts:1202
target: 'writeConcern',
transform({ values: [value], options }) {
if (isRecord(value) || value instanceof WriteConcern) {
return WriteConcern.fromOptions({
writeConcern: {
...options.writeConcern,
...value
}
});
} else if (value === 'majority' || typeof value === 'number') {
return WriteConcern.fromOptions({
writeConcern: {
...options.writeConcern,
w: value
}
});
}
throw new MongoParseError(`Invalid WriteConcern cannot parse: ${JSON.stringify(value)}`);
}
},
wtimeout: {
deprecated: 'Please use wtimeoutMS instead',
target: 'writeConcern',
transform({ values: [value], options }) {
const wc = WriteConcern.fromOptions({
writeConcern: {
...options.writeConcern,
wtimeout: getUIntFromOptions('wtimeout', value)
}
});
if (wc) return wc;
throw new MongoParseError(`Cannot make WriteConcern from wtimeout`);
}
} as OptionDescriptor,
wtimeoutMS: {
target: 'writeConcern',View on GitHub (pinned to dce7939f86)