{"record":{"id":"1d614c974a1d6bd0","repo":"Automattic/mongoose","slug":"path-this-path-may-not-have-index-set-to-fa-1d614c","errorCode":null,"errorMessage":"Path \"${this.path}\" may not have `index` set to false and `sparse` set to true","messagePattern":"Path \"(.+?)\" may not have `index` set to false and `sparse` set to true","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schemaType.js","lineNumber":106,"sourceCode":"      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;\n      }\n\n      const opts = Array.isArray(val) ? val : [val];\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schemaType.js#L88-L124","documentation":"Same options-processing check as the unique variant, but for sparse indexes: a path defined with `index: false` while an earlier option already registered an index object with `sparse: true` is rejected (lib/schemaType.js:106), because a sparse index is still an index mongoose would have to create.","triggerScenarios":"`new Schema({ ref: { type: String, sparse: true, index: false } })` — sparse registers an index object, then `index: false` contradicts it.","commonSituations":"Optional fields made sparse, combined with blanket per-path `index: false`; schema config merges between teams.","solutions":["Remove `index: false` from the path","Use `index: { sparse: true }` as the single declaration","Control auto index creation with `autoIndex: false` at the connection/schema level instead of per-path"],"exampleFix":"// before\nnew Schema({ ref: { type: String, sparse: true, index: false } }); // throws\n\n// after\nnew Schema({ ref: { type: String, index: { sparse: true } } });","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":["Keep sparse/unique declarations in the single `index` object form","Control index creation with schema/connection autoIndex flags"],"tags":["mongoose","schema","index","sparse","options"],"backgroundTag":"invalid-schema-option","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}