{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/read_preference.ts#L97-L133","documentation":"Thrown by the ReadPreference constructor when mode is 'primary' AND options.maxStalenessSeconds is set (truthy). Max staleness only makes sense for reads that can go to secondaries; the primary has no staleness concept, so combining them is contradictory and rejected. MongoInvalidArgumentError.","triggerScenarios":"new ReadPreference('primary', undefined, { maxStalenessSeconds: 90 }); connection strings with both readPreference=primary and maxStalenessSeconds; per-operation options that set both via fromOptions/translate; generic option objects reused across modes.","commonSituations":"Shared read-preference option objects reused for both primary and secondary paths; config that sets maxStalenessSeconds globally and then a specific call forces primary; connection-string builders that concatenate independent settings.","solutions":["Drop maxStalenessSeconds when using primary reads.","If you want staleness-bounded reads, use a non-primary mode (primaryPreferred/secondary/secondaryPreferred/nearest).","Separate the primary and secondary read-preference configs so the staleness option isn't carried into primary.","Validate option combinations before constructing a ReadPreference."],"exampleFix":"// before\nnew ReadPreference('primary', undefined, { maxStalenessSeconds: 90 });\n// after\nReadPreference.primary; // or\nnew ReadPreference('secondaryPreferred', undefined, { maxStalenessSeconds: 90 });","handlingStrategy":"validation","validationCode":"if (mode === 'primary' && options?.maxStalenessSeconds) {\n  throw new Error('primary cannot be combined with maxStalenessSeconds');\n}\nnew ReadPreference(mode, tags, options);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Separate primary and secondary read-preference configs.","Drop maxStalenessSeconds when forcing primary reads.","Validate option combinations in shared config builders."],"tags":["read-preference","max-staleness","primary","validation","argument-error"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}