{"record":{"id":"717d96845bbbc164","repo":"Automattic/mongoose","slug":"collection-createindex-unimplemented-by-driver","errorCode":null,"errorMessage":"Collection#createIndex unimplemented by driver","messagePattern":"Collection#createIndex unimplemented by driver","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/collection.js","lineNumber":152,"sourceCode":"    _this.emitter.emit('queue');\n  });\n  return this;\n};\n\n/**\n * Abstract method that drivers must implement.\n */\n\nCollection.prototype.ensureIndex = function() {\n  throw new Error('Collection#ensureIndex unimplemented by driver');\n};\n\n/**\n * Abstract method that drivers must implement.\n */\n\nCollection.prototype.createIndex = function() {\n  throw new Error('Collection#createIndex unimplemented by driver');\n};\n\n/**\n * Abstract method that drivers must implement.\n */\n\nCollection.prototype.findAndModify = function() {\n  throw new Error('Collection#findAndModify unimplemented by driver');\n};\n\n/**\n * Abstract method that drivers must implement.\n */\n\nCollection.prototype.findOneAndUpdate = function() {\n  throw new Error('Collection#findOneAndUpdate unimplemented by driver');\n};\n","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/collection.js#L134-L170","documentation":"createIndex is declared abstract on mongoose's base Collection and throws unless the attached driver implements it. Seeing this Error means the collection you called is not backed by a real driver implementation -- a bare Collection instance, a partial mock, or a custom driver that skipped the method.","triggerScenarios":"Model.collection.createIndex(...) where Model.collection was replaced by a test double; a Collection constructed directly via new Collection(name, conn) with no native collection; a custom driver subclass missing createIndex.","commonSituations":"Unit tests with hand-rolled collection fakes implementing only CRUD; custom non-MongoDB backends reusing mongoose's Collection; calling index APIs before a driver attached its implementation.","solutions":["Use Model.createIndexes() / Model.syncIndexes() or schema index definitions instead of Model.collection.createIndex()","Ensure the model is connected to a real connection (await mongoose.connect) before touching Model.collection","Implement createIndex on the custom driver, or complete the test double"],"exampleFix":"// before\nawait Model.collection.createIndex({ sku: 1 }, { unique: true }); // mock collection\n\n// after\nconst schema = new Schema({ sku: { type: String, unique: true } });\nawait Model.syncIndexes();","handlingStrategy":"type-guard","validationCode":"const BaseCollection = require('mongoose/lib/collection');\nfunction assertCreateIndexAvailable(coll) {\n  if (coll.createIndex === BaseCollection.prototype.createIndex) {\n    throw new Error('createIndex not implemented by this collection/driver');\n  }\n}\nassertCreateIndexAvailable(Model.collection);","typeGuard":"const BaseCollection = require('mongoose/lib/collection');\nfunction isDriverBackedCollection(coll) {\n  return typeof coll.createIndex === 'function' &&\n    coll.createIndex !== BaseCollection.prototype.createIndex;\n}","tryCatchPattern":null,"preventionTips":["Prefer model-level index APIs over Model.collection","Give test fakes the full method surface, or use an in-memory MongoDB","Fail fast on missing driver methods instead of catching broadly"],"tags":["mongoose","driver","index","testing"],"backgroundTag":"driver-method-unimplemented","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}