{"record":{"id":"1ea7963a71268233","repo":"mongodb/node-mongodb-native","slug":"primary-read-preference-cannot-be-combined-with-ta","errorCode":null,"errorMessage":"Primary read preference cannot be combined with tags","messagePattern":"Primary read preference cannot be combined with tags","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/read_preference.ts","lineNumber":111,"sourceCode":"    }\n\n    this.mode = mode;\n    this.tags = tags;\n    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 {","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/read_preference.ts#L93-L129","documentation":"Thrown by the ReadPreference constructor (src/read_preference.ts:111) when mode is 'primary' and a non-empty tags array is supplied. Per the MongoDB read preference specification, primary reads always target the primary, so tag-based routing is meaningless and explicitly forbidden. MongoInvalidArgumentError.","triggerScenarios":"new ReadPreference('primary', [{ region: 'us' }]), or a connection string like mongodb://host/?readPreference=primary&readPreferenceTags=region:us, or per-operation options combining primary with tags.","commonSituations":"Applying a global tag configuration unconditionally across all read preferences, including primary; copy-pasted connection strings.","solutions":["Remove tags when using primary mode.","Use primaryPreferred if you want primary-first with tag fallback.","Audit connection strings and option builders to drop tags for primary."],"exampleFix":"// before\nnew ReadPreference('primary', [{ region: 'us' }]);\n// after\nnew ReadPreference('primary');","handlingStrategy":"validation","validationCode":"function buildRP(mode, tags) {\n  if (mode === 'primary' && tags != null && tags.length > 0) {\n    throw new Error('primary cannot have tags');\n  }\n  return new ReadPreference(mode, tags);\n}","typeGuard":"function primaryHasTags(mode, tags): boolean {\n  return mode === 'primary' && Array.isArray(tags) && tags.length > 0;\n}","tryCatchPattern":"try {\n  new ReadPreference(mode, tags);\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /cannot be combined with tags/.test(e.message)) {\n    // strip tags and retry\n  }\n  throw e;\n}","preventionTips":["Build read-preference options conditionally on mode.","Audit connection strings for readPreferenceTags combined with primary.","Centralize read-preference construction in a single helper."],"tags":["read-preference","validation","tags","invalid-argument"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}