{"record":{"id":"aa4c585f47918137","repo":"mongodb/node-mongodb-native","slug":"primary-read-preference-cannot-be-combined-with-ma","errorCode":null,"errorMessage":"Primary read preference cannot be combined with maxStalenessSeconds","messagePattern":"Primary read preference cannot be combined with maxStalenessSeconds","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/read_preference.ts","lineNumber":115,"sourceCode":"    this.hedge = options?.hedge;\n    this.maxStalenessSeconds = undefined;\n\n    options = options ?? {};\n    if (options.maxStalenessSeconds != null) {\n      if (options.maxStalenessSeconds <= 0) {\n        throw new MongoInvalidArgumentError('maxStalenessSeconds must be a positive integer');\n      }\n\n      this.maxStalenessSeconds = options.maxStalenessSeconds;\n    }\n\n    if (this.mode === ReadPreference.PRIMARY) {\n      if (this.tags && Array.isArray(this.tags) && this.tags.length > 0) {\n        throw new MongoInvalidArgumentError('Primary read preference cannot be combined with tags');\n      }\n\n      if (this.maxStalenessSeconds) {\n        throw new MongoInvalidArgumentError(\n          'Primary read preference cannot be combined with maxStalenessSeconds'\n        );\n      }\n\n      if (this.hedge) {\n        throw new MongoInvalidArgumentError(\n          'Primary read preference cannot be combined with hedge'\n        );\n      }\n    }\n  }\n\n  // Support the deprecated `preference` property introduced in the porcelain layer\n  get preference(): ReadPreferenceMode {\n    return this.mode;\n  }\n\n  static fromString(mode: string): ReadPreference {","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/read_preference.ts#L97-L133","documentation":"Thrown by the ReadPreference constructor (src/read_preference.ts:115) when mode is 'primary' and options.maxStalenessSeconds is set. Staleness is only meaningful for secondary reads; the spec forbids combining it with primary. MongoInvalidArgumentError.","triggerScenarios":"new ReadPreference('primary', undefined, { maxStalenessSeconds: 90 }), or connection string mongodb://host/?readPreference=primary&maxStalenessSeconds=90.","commonSituations":"Inheriting a global maxStalenessSeconds config across all read preference modes, or templated connection strings.","solutions":["Drop maxStalenessSeconds when using primary.","Switch to primaryPreferred or secondaryPreferred if staleness limiting is intended.","Conditionally build the options object based on mode."],"exampleFix":"// before\nnew ReadPreference('primary', undefined, { maxStalenessSeconds: 90 });\n// after\nnew ReadPreference('primary');","handlingStrategy":"validation","validationCode":"function buildRP(mode, options) {\n  if (mode === 'primary' && options?.maxStalenessSeconds != null) {\n    throw new Error('primary cannot have maxStalenessSeconds');\n  }\n  return new ReadPreference(mode, undefined, options);\n}","typeGuard":"function primaryHasMaxStaleness(mode, opts): boolean {\n  return mode === 'primary' && opts?.maxStalenessSeconds != null;\n}","tryCatchPattern":"try {\n  new ReadPreference(mode, undefined, opts);\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /maxStalenessSeconds/.test(e.message)) {\n    // remove staleness option and retry\n  }\n  throw e;\n}","preventionTips":["Do not apply global maxStalenessSeconds across all modes.","Validate option/mode combinations in a shared builder.","Review templated connection strings for this conflict."],"tags":["read-preference","validation","max-staleness","invalid-argument"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}