mongodb/node-mongodb-native · error · MongoParseError

Cannot make read preference from ${JSON.stringify(value)}

Error message

Cannot make read preference from ${JSON.stringify(value)}

What it means

When `readPreference` is an object with a `mode` key, the transform (src/connection_string.ts:1017-1023) calls `ReadPreference.fromOptions`; if that returns null (invalid mode or conflicting combination), it throws 'Cannot make read preference from ...'.

Source

Thrown at src/connection_string.ts:1023

      });
    }
  },
  readPreference: {
    default: ReadPreference.primary,
    transform({ values: [value], options }) {
      if (value instanceof ReadPreference) {
        return ReadPreference.fromOptions({
          readPreference: { ...options.readPreference, ...value },
          ...value
        });
      }
      if (isRecord(value, ['mode'] as const)) {
        const rp = ReadPreference.fromOptions({
          readPreference: { ...options.readPreference, ...value },
          ...value
        });
        if (rp) return rp;
        else throw new MongoParseError(`Cannot make read preference from ${JSON.stringify(value)}`);
      }
      if (typeof value === 'string') {
        const rpOpts = {
          hedge: options.readPreference?.hedge,
          maxStalenessSeconds: options.readPreference?.maxStalenessSeconds
        };
        return new ReadPreference(
          value as ReadPreferenceMode,
          options.readPreference?.tags,
          rpOpts
        );
      }
      throw new MongoParseError(`Unknown ReadPreference value: ${value}`);
    }
  },
  readPreferenceTags: {
    target: 'readPreference',
    transform({

View on GitHub (pinned to 3366c21a63)

Solutions

  1. Use a valid mode: 'primary','primaryPreferred','secondary','secondaryPreferred','nearest'
  2. Remove maxStalenessSeconds and tags when using 'primary'
  3. Pass readPreference as a string or a ReadPreference instance instead of an object

Example fix

// before
new MongoClient(uri, { readPreference: { mode: 'primary', maxStalenessSeconds: 5 } });
// after
new MongoClient(uri, { readPreference: { mode: 'nearest', maxStalenessSeconds: 5 } });
Defensive patterns

Strategy: validation

Validate before calling

const VALID_MODES = ['primary','primaryPreferred','secondary','secondaryPreferred','nearest'];
function isValidReadPreferenceObject(v) {
  if (!v || typeof v !== 'object') return false;
  if (!VALID_MODES.includes(v.mode)) return false;
  if (v.mode === 'primary' && (v.maxStalenessSeconds != null || (v.tags && v.tags.length))) return false;
  return true;
}
if (options.readPreference && typeof options.readPreference === 'object' && !isValidReadPreferenceObject(options.readPreference)) {
  throw new TypeError('Invalid readPreference object');
}

Type guard

const VALID_MODES = new Set(['primary','primaryPreferred','secondary','secondaryPreferred','nearest']);
function isReadPreferenceMode(m) {
  return VALID_MODES.has(m);
}

Prevention

When it happens

Trigger: `{ readPreference: { mode: 'fastest' } }` (invalid mode); `{ readPreference: { mode: 'primary', maxStalenessSeconds: 5 } }` (maxStaleness is invalid with primary); `{ readPreference: { mode: 'primary', tags: [{region:'eu'}] } }` (tags invalid with primary).

Common situations: Typos in mode; combining 'primary' with secondary-only options like tags or maxStalenessSeconds; stale config from a replica-set-only deployment.

Related errors


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