mongodb/node-mongodb-native · error · MongoParseError
Invalid WriteConcern cannot parse: ${JSON.stringify(value)}
Error message
Invalid WriteConcern cannot parse: ${JSON.stringify(value)} What it means
The `writeConcern` option accepts a `WriteConcern` instance, a plain object, the string `'majority'`, or a number (used as `w`). The transform (src/connection_string.ts:1183-1203) throws 'Invalid WriteConcern cannot parse' for any other type.
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 3366c21a63)
Solutions
- Pass an object: `{ w: 'majority', j: true, wtimeoutMS: 1000 }`
- Or `writeConcern: 'majority'`
- Or `writeConcern: 1` (numeric w)
- Use a WriteConcern instance from `new WriteConcern(...)`
Example fix
// before
new MongoClient(uri, { writeConcern: 'all' });
// after
new MongoClient(uri, { writeConcern: { w: 'majority', j: true, wtimeoutMS: 1000 } }); Defensive patterns
Strategy: type-guard
Validate before calling
import { WriteConcern } from 'mongodb';
function isWriteConcernInput(v) {
return (
v instanceof WriteConcern ||
(v != null && typeof v === 'object') ||
v === 'majority' ||
typeof v === 'number'
);
}
if (options.writeConcern != null && !isWriteConcernInput(options.writeConcern)) {
throw new TypeError('writeConcern must be an object, WriteConcern, majority, or number');
} Type guard
import { WriteConcern } from 'mongodb';
function isWriteConcernLike(v) {
return v instanceof WriteConcern || (v != null && typeof v === 'object') || v === 'majority' || typeof v === 'number';
} Prevention
- Pass writeConcern as an object { w, j, wtimeoutMS }
- Only the string 'majority' is allowed; other strings are rejected
- Use new WriteConcern(...) for explicit construction
When it happens
Trigger: `{ writeConcern: 'all' }` (string other than 'majority'); `{ writeConcern: true }`; `{ writeConcern: [1,2] }`; `{ writeConcern: 'majority,local' }`.
Common situations: Passing a read-concern-style level like 'local'/'all'; confusing write concern with read concern levels; boolean j flag misuse; array from parsed input.
Related errors
- Descriptors missing a type must define a transform
- ${name} must be an object with 'username' and 'password' pro
- authMechanism one of ${mechanisms}, got ${value}
- Invalid `serverApi` property; must specify a version from th
- Invalid server API version=${versionToValidate}; must be in
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/6f113727cc425381.json.
Report an issue: GitHub.