{"record":{"id":"a2e64c16941ab352","repo":"Automattic/mongoose","slug":"path-this-path-may-not-have-index-set-to-fa","errorCode":null,"errorMessage":"Path \"${this.path}\" may not have `index` set to false and `unique` set to true","messagePattern":"Path \"(.+?)\" may not have `index` set to false and `unique` set to true","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schemaType.js","lineNumber":102,"sourceCode":"\n  const keys = Object.keys(this.options);\n  for (const prop of keys) {\n    if (prop === 'cast') {\n      if (Array.isArray(this.options[prop])) {\n        this.castFunction.apply(this, this.options[prop]);\n      } else {\n        this.castFunction(this.options[prop]);\n      }\n      continue;\n    }\n    if (utils.hasUserDefinedProperty(this.options, prop) && typeof this[prop] === 'function') {\n      // { unique: true, index: true }\n      if (prop === 'index' && this._index) {\n        if (options.index === false) {\n          const index = this._index;\n          if (typeof index === 'object' && index != null) {\n            if (index.unique) {\n              throw new MongooseError('Path \"' + this.path + '\" may not have `index` ' +\n                'set to false and `unique` set to true');\n            }\n            if (index.sparse) {\n              throw new MongooseError('Path \"' + this.path + '\" may not have `index` ' +\n                'set to false and `sparse` set to true');\n            }\n          }\n\n          this._index = false;\n        }\n        continue;\n      }\n\n      const val = options[prop];\n      // Special case so we don't screw up array defaults, see gh-5780\n      if (prop === 'default') {\n        this.default(val);\n        continue;","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schemaType.js#L84-L120","documentation":"While a SchemaType processes its path options, mongoose detects the contradictory combination `index: false` together with an already-registered index object carrying `unique: true` (lib/schemaType.js:102). MongoDB implements `unique` via an index, so disabling the index while requiring uniqueness is rejected at schema build time.","triggerScenarios":"`new Schema({ email: { type: String, unique: true, index: false } })` — the `unique` option registers `_index = { unique: true }`, then `index: false` contradicts it.","commonSituations":"Adding `index: false` per-path to stop auto index creation in CI/dev; merging schema fragments where one side disables indexes and the other requires uniqueness; overriding an inherited index.","solutions":["Remove `index: false` from the path definition","To stop automatic index creation globally use `mongoose.connect(uri, { autoIndex: false })` or `schema.set('autoIndex', false)`","Express the intent with a single form: `index: { unique: true }`"],"exampleFix":"// before\nnew Schema({ email: { type: String, unique: true, index: false } }); // throws\n\n// after\nnew Schema({ email: { type: String, index: { unique: true } } });\n// and if auto-indexing is unwanted: schema.set('autoIndex', false);","handlingStrategy":"validation","validationCode":"function assertIndexOptionsCompatible(def) {\n  if (def.index === false && (def.unique === true || def.sparse === true || def.text === true)) {\n    throw new Error('index:false cannot be combined with unique/sparse/text; use autoIndex:false instead');\n  }\n}","typeGuard":"const hasIndexOptionConflict = def => def?.index === false && Boolean(def?.unique || def?.sparse || def?.text);","tryCatchPattern":null,"preventionTips":["Never express 'no indexes' as per-path index:false; use autoIndex:false","Lint schema definitions for index:false co-occurring with unique/sparse/text","Write uniqueness as `index: { unique: true }` in one place only"],"tags":["mongoose","schema","index","unique","options"],"backgroundTag":"invalid-schema-option","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}