{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/read_preference.ts#L93-L129","documentation":"Thrown by the ReadPreference constructor when mode is 'primary' AND a non-empty tags array is supplied. Tags target specific replica-set members for reads, but primary reads always go to the single primary - targeting tags with primary is contradictory and disallowed. MongoInvalidArgumentError. (An empty tags array is permitted; only a non-empty one triggers this.)","triggerScenarios":"new ReadPreference('primary', [{ region: 'us-east' }]); passing readPreference=primary together with readPreferenceTags in a connection string; per-operation options that combine primary with tags via fromOptions/translate.","commonSituations":"Generic config templates that always set tags; switching a mode to primary without clearing tags; connection strings assembled from parts that independently set both.","solutions":["If you need primary reads, remove the tags.","If you need tag-targeted reads, switch the mode to primaryPreferred/secondary/secondaryPreferred/nearest.","Audit connection strings and option builders so tags and primary are not set together.","Use ReadPreference.primary (no tags) for unambiguous primary reads."],"exampleFix":"// before\nnew ReadPreference('primary', [{ region: 'us-east' }]);\n// after\nReadPreference.primary; // or\nnew ReadPreference('secondary', [{ region: 'us-east' }]);","handlingStrategy":"validation","validationCode":"if (mode === 'primary' && Array.isArray(tags) && tags.length > 0) {\n  throw new Error('primary cannot be combined with tags');\n}\nnew ReadPreference(mode, tags, options);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use ReadPreference.primary (no tags) for unambiguous primary reads.","Don't reuse a tags-configured option object for primary reads.","Audit connection strings so readPreference=primary and readPreferenceTags aren't both set."],"tags":["read-preference","tags","primary","validation","argument-error"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}