{"record":{"id":"f5770224eba13fff","repo":"Automattic/mongoose","slug":"mongoose-f57702","errorCode":"MONGOOSE","errorMessage":"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","messagePattern":"mongoose: Cannot specify a custom index on `_id` for model name \"(.+?)\", MongoDB does not allow overwriting the default `_id` index\\. See https://bit\\.ly/mongodb-id-index","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/model.js","lineNumber":1712,"sourceCode":" * ignore\n */\n\nfunction _ensureIndexes(model, options, callback) {\n  const indexes = Array.isArray(options?.toCreate) ? options.toCreate : model.schema.indexes();\n  let indexError;\n\n  options = options || {};\n  const done = function(err) {\n    if (err && !model.$caught) {\n      model.emit('error', err);\n    }\n    model.emit('index', err || indexError);\n    callback && callback(err || indexError);\n  };\n\n  for (const index of indexes) {\n    if (isDefaultIdIndex(index)) {\n      utils.warn('mongoose: Cannot specify a custom index on `_id` for ' +\n        'model name \"' + model.modelName + '\", ' +\n        'MongoDB does not allow overwriting the default `_id` index. See ' +\n        'https://bit.ly/mongodb-id-index');\n    }\n  }\n\n  // Check for duplicate index definitions (gh-15056)\n  const seenIndexes = [];\n  for (const index of indexes) {\n    const fields = index[0];\n    const indexOptions = index[1];\n    if (indexOptions.name == null) {\n      for (const existingIndex of seenIndexes) {\n        if (existingIndex[1].name == null && isIndexSpecEqual(existingIndex[0], fields)) {\n          utils.warn('mongoose: Duplicate schema index on ' + JSON.stringify(fields) +\n            ' for model \"' + model.modelName + '\". ' +\n            'This is often due to declaring an index using both \"index: true\" and \"schema.index()\". ' +\n            'Please remove the duplicate index definition.');","sourceCodeStart":1694,"sourceCodeEnd":1730,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/model.js#L1694-L1730","documentation":"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.","triggerScenarios":"Thrown at lib/model.js:1712 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}