{"record":{"id":"f1200c31db9791e3","repo":"Automattic/mongoose","slug":"mongodb-prohibits-index-creation-on-connections-th","errorCode":null,"errorMessage":"MongoDB prohibits index creation on connections that read from non-primary replicas.  Connections that set \"readPreference\" to \"secondary\" or \"secondaryPreferred\" may not opt-in to the following connection options: autoCreate, autoIndex","messagePattern":"MongoDB prohibits index creation on connections that read from non-primary replicas\\.  Connections that set \"readPreference\" to \"secondary\" or \"secondaryPreferred\" may not opt-in to the following connection options: autoCreate, autoIndex","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/helpers/processConnectionOptions.js","lineNumber":57,"sourceCode":"function setsSecondaryRead(prefString) {\n  return !!(prefString === 'secondary' || prefString === 'secondaryPreferred');\n}\n\nfunction getUriReadPreference(connectionString) {\n  const exp = /(?:&|\\?)readPreference=(\\w+)(?:&|$)/;\n  const match = exp.exec(connectionString);\n  return match ? match[1] : null;\n}\n\nfunction defaultIndexOptsToFalse(opts) {\n  opts.config = { autoIndex: false };\n  opts.autoCreate = false;\n  opts.autoIndex = false;\n  return opts;\n}\n\nfunction throwReadPreferenceError() {\n  throw new MongooseError(\n    'MongoDB prohibits index creation on connections that read from ' +\n            'non-primary replicas.  Connections that set \"readPreference\" to \"secondary\" or ' +\n            '\"secondaryPreferred\" may not opt-in to the following connection options: ' +\n            'autoCreate, autoIndex'\n  );\n}\n\nmodule.exports = processConnectionOptions;\n","sourceCodeStart":39,"sourceCodeEnd":66,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/helpers/processConnectionOptions.js#L39-L66","documentation":"Error \"MongoDB prohibits index creation on connections that read from non-primary replicas.  Connections that set \"readPreference\" to \"secondary\" or \"secondaryPreferred\" may not opt-in to the following connection options: autoCreate, autoIndex\" thrown in Automattic/mongoose.","triggerScenarios":"Thrown at lib/helpers/processConnectionOptions.js:57 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}