Automattic/mongoose · error · MongooseError

Must set `versionKey` if using `optimisticConcurrency`

Error message

Must set `versionKey` if using `optimisticConcurrency`

What it means

Error "Must set `versionKey` if using `optimisticConcurrency`" thrown in Automattic/mongoose.

Source

Thrown at lib/schema.js:654

  options.strict ??= defaultStrict;
  options.strictQuery ??= defaultStrictQuery;
  options.id ??= defaultId;

  if (options.versionKey && typeof options.versionKey !== 'string') {
    throw new MongooseError('`versionKey` must be falsy or string, got `' + (typeof options.versionKey) + '`');
  }

  if (typeof options.read === 'string') {
    options.read = handleReadPreferenceAliases(options.read);
  } else if (Array.isArray(options.read) && typeof options.read[0] === 'string') {
    options.read = {
      mode: handleReadPreferenceAliases(options.read[0]),
      tags: options.read[1]
    };
  }

  if (options.optimisticConcurrency && !options.versionKey) {
    throw new MongooseError('Must set `versionKey` if using `optimisticConcurrency`');
  }

  return options;
};

/**
 * Inherit a Schema by applying a discriminator on an existing Schema.
 *
 *
 * #### Example:
 *
 *     const eventSchema = new mongoose.Schema({ timestamp: Date }, { discriminatorKey: 'kind' });
 *
 *     const clickedEventSchema = new mongoose.Schema({ element: String }, { discriminatorKey: 'kind' });
 *     const ClickedModel = eventSchema.discriminator('clicked', clickedEventSchema);
 *
 *     const Event = mongoose.model('Event', eventSchema);
 *

View on GitHub (pinned to 49cdab0136)

When it happens

Trigger: Thrown at lib/schema.js:654 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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