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

  1. Pass an object: `{ w: 'majority', j: true, wtimeoutMS: 1000 }`
  2. Or `writeConcern: 'majority'`
  3. Or `writeConcern: 1` (numeric w)
  4. 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

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


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/6f113727cc425381.json. Report an issue: GitHub.