{"record":{"id":"165a3cb1904bacc0","repo":"Automattic/mongoose","slug":"cannot-overwrite-schema-indextypes","errorCode":null,"errorMessage":"Cannot overwrite Schema.indexTypes","messagePattern":"Cannot overwrite Schema\\.indexTypes","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schema.js","lineNumber":2531,"sourceCode":"};\n\nconst indexTypes = '2d 2dsphere hashed text'.split(' ');\n\n/**\n * The allowed index types\n *\n * @property {string[]} indexTypes\n * @memberOf Schema\n * @static\n * @api public\n */\n\nObject.defineProperty(Schema, 'indexTypes', {\n  get: function() {\n    return indexTypes;\n  },\n  set: function() {\n    throw new MongooseError('Cannot overwrite Schema.indexTypes');\n  }\n});\n\n/**\n * Returns a list of indexes that this schema declares, via `schema.index()` or by `index: true` in a path's options.\n * Indexes are expressed as an array `[spec, options]`.\n *\n * #### Example:\n *\n *     const userSchema = new Schema({\n *       email: { type: String, required: true, unique: true },\n *       registeredAt: { type: Date, index: true }\n *     });\n *\n *     // [ [ { email: 1 }, { unique: true } ],\n *     //   [ { registeredAt: 1 }, {} ] ]\n *     userSchema.indexes();\n *","sourceCodeStart":2513,"sourceCodeEnd":2549,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema.js#L2513-L2549","documentation":"Schema.indexTypes is a read-only static listing the valid index types ('2d', '2dsphere', 'hashed', 'text'); mongoose installs a getter with a throwing setter so this lookup table cannot be silently replaced. Any assignment to the property raises this error.","triggerScenarios":"A direct write such as `mongoose.Schema.indexTypes = [...]`; helper code that copies statics via Object.assign(Schema, src); monkey-patching or test stubbing that treats class statics as writable.","commonSituations":"Attempting to extend the allowed index types (not supported - index types come from the server/driver); generic merge/extend utilities applied to the Schema class; migration scripts that clone mongoose internals.","solutions":["Delete the assignment - indexTypes is informational and not user-configurable.","Create exotic indexes directly on the collection with the driver (collection.createIndex) instead.","Audit Object.assign/extend calls targeting Schema and skip read-only statics (getter without setter)."],"exampleFix":"// before\nmongoose.Schema.indexTypes = ['2d', '2dsphere', 'myIndexType'];\n\n// after\nschema.index({ loc: '2dsphere' }); // declare supported index types per-path\nawait model.collection.createIndex({ a: 1 }, { unique: true }); // exotic options go to the driver","handlingStrategy":"validation","validationCode":"const safeAssignStatics = (target, source) => {\n  for (const k of Object.keys(source)) {\n    const d = Object.getOwnPropertyDescriptor(target, k);\n    if (d && d.get && !d.set) continue; // skip read-only statics like indexTypes\n    target[k] = source[k];\n  }\n};","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat mongoose class statics as read-only; check docs before assigning.","When stubbing in tests, use jest.spyOn on getters instead of assignment.","Audit generic Object.assign/extend helpers that target library classes."],"tags":["read-only","static-api","monkey-patching"],"backgroundTag":"readonly-property-assignment","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}