Automattic/mongoose · error

MONGOOSE

MONGOOSE

Error message

mongoose: Cannot specify a custom index on `_id` for model name "${model.modelName}", MongoDB does not allow overwriting the default `_id` index. See https://bit.ly/mongodb-id-index

What it means

Error "mongoose: Cannot specify a custom index on `_id` for model name "${model.modelName}", MongoDB does not allow overwriting the default `_id` index. See https://bit.ly/mongodb-id-index" thrown in Automattic/mongoose.

Source

Thrown at lib/model.js:1712

 * ignore
 */

function _ensureIndexes(model, options, callback) {
  const indexes = Array.isArray(options?.toCreate) ? options.toCreate : model.schema.indexes();
  let indexError;

  options = options || {};
  const done = function(err) {
    if (err && !model.$caught) {
      model.emit('error', err);
    }
    model.emit('index', err || indexError);
    callback && callback(err || indexError);
  };

  for (const index of indexes) {
    if (isDefaultIdIndex(index)) {
      utils.warn('mongoose: Cannot specify a custom index on `_id` for ' +
        'model name "' + model.modelName + '", ' +
        'MongoDB does not allow overwriting the default `_id` index. See ' +
        'https://bit.ly/mongodb-id-index');
    }
  }

  // Check for duplicate index definitions (gh-15056)
  const seenIndexes = [];
  for (const index of indexes) {
    const fields = index[0];
    const indexOptions = index[1];
    if (indexOptions.name == null) {
      for (const existingIndex of seenIndexes) {
        if (existingIndex[1].name == null && isIndexSpecEqual(existingIndex[0], fields)) {
          utils.warn('mongoose: Duplicate schema index on ' + JSON.stringify(fields) +
            ' for model "' + model.modelName + '". ' +
            'This is often due to declaring an index using both "index: true" and "schema.index()". ' +
            'Please remove the duplicate index definition.');

View on GitHub (pinned to 49cdab0136)

When it happens

Trigger: Thrown at lib/model.js:1712 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/f5770224eba13fff. Report an issue: GitHub.