Automattic/mongoose · error · MongooseError

Path "${this.path}" may not have `index` set to false and `u

Error message

Path "${this.path}" may not have `index` set to false and `unique` set to true

What it means

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.

Source

Thrown at lib/schemaType.js:102

  const keys = Object.keys(this.options);
  for (const prop of keys) {
    if (prop === 'cast') {
      if (Array.isArray(this.options[prop])) {
        this.castFunction.apply(this, this.options[prop]);
      } else {
        this.castFunction(this.options[prop]);
      }
      continue;
    }
    if (utils.hasUserDefinedProperty(this.options, prop) && typeof this[prop] === 'function') {
      // { unique: true, index: true }
      if (prop === 'index' && this._index) {
        if (options.index === false) {
          const index = this._index;
          if (typeof index === 'object' && index != null) {
            if (index.unique) {
              throw new MongooseError('Path "' + this.path + '" may not have `index` ' +
                'set to false and `unique` set to true');
            }
            if (index.sparse) {
              throw new MongooseError('Path "' + this.path + '" may not have `index` ' +
                'set to false and `sparse` set to true');
            }
          }

          this._index = false;
        }
        continue;
      }

      const val = options[prop];
      // Special case so we don't screw up array defaults, see gh-5780
      if (prop === 'default') {
        this.default(val);
        continue;

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Remove `index: false` from the path definition
  2. To stop automatic index creation globally use `mongoose.connect(uri, { autoIndex: false })` or `schema.set('autoIndex', false)`
  3. Express the intent with a single form: `index: { unique: true }`

Example fix

// before
new Schema({ email: { type: String, unique: true, index: false } }); // throws

// after
new Schema({ email: { type: String, index: { unique: true } } });
// and if auto-indexing is unwanted: schema.set('autoIndex', false);
Defensive patterns

Strategy: validation

Validate before calling

function assertIndexOptionsCompatible(def) {
  if (def.index === false && (def.unique === true || def.sparse === true || def.text === true)) {
    throw new Error('index:false cannot be combined with unique/sparse/text; use autoIndex:false instead');
  }
}

Type guard

const hasIndexOptionConflict = def => def?.index === false && Boolean(def?.unique || def?.sparse || def?.text);

Prevention

When it happens

Trigger: `new Schema({ email: { type: String, unique: true, index: false } })` — the `unique` option registers `_index = { unique: true }`, then `index: false` contradicts it.

Common situations: 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.

Related errors


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/a2e64c16941ab352. Report an issue: GitHub.