{"record":{"id":"562a4c6470b57b92","repo":"Automattic/mongoose","slug":"collection-ensureindex-unimplemented-by-driver","errorCode":null,"errorMessage":"Collection#ensureIndex unimplemented by driver","messagePattern":"Collection#ensureIndex unimplemented by driver","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/collection.js","lineNumber":144,"sourceCode":"      method[0].apply(this, method[1]);\n    } else {\n      this[method[0]].apply(this, method[1]);\n    }\n  }\n  this.queue = [];\n  const _this = this;\n  immediate(function() {\n    _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","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/collection.js#L126-L162","documentation":"lib/collection.js defines an abstract Collection base whose methods throw 'unimplemented by driver' -- ensureIndex is one. The real node-mongodb-native driver subclasses Collection and implements these, so this Error means the collection object in use (a mock, a bare Collection, or a custom driver) never provided an implementation.","triggerScenarios":"Calling Model.collection.ensureIndex({ email: 1 }, { unique: true }) when Model.collection is a stubbed/mocked collection; custom drivers that extend mongoose's Collection without implementing ensureIndex; code written against ancient drivers where ensureIndex was native.","commonSituations":"Test doubles replacing Model.collection (mongoose-mock style) that implement only find/insert; custom storage adapters built on mongoose internals; legacy scripts calling ensureIndex directly.","solutions":["Call createIndex instead -- ensureIndex is the pre-3.0 MongoDB API name","Prefer schema-level indexes (field: { index: true }) or Model.syncIndexes()/Model.createIndexes()","If you own the driver or mock, implement ensureIndex by delegating to createIndex"],"exampleFix":"// before\nawait Model.collection.ensureIndex({ email: 1 }, { unique: true });\n\n// after\nawait Model.collection.createIndex({ email: 1 }, { unique: true });\n// or declare it on the schema: new Schema({ email: { type: String, unique: true } })","handlingStrategy":"type-guard","validationCode":"const BaseCollection = require('mongoose/lib/collection');\nfunction collectionSupports(coll, method) {\n  return typeof coll[method] === 'function' &&\n    coll[method] !== BaseCollection.prototype[method];\n}\nif (!collectionSupports(Model.collection, 'ensureIndex')) {\n  throw new Error('Driver does not implement ensureIndex; use createIndex');\n}","typeGuard":"const BaseCollection = require('mongoose/lib/collection');\nfunction hasRealCreateIndex(coll) {\n  return typeof coll.createIndex === 'function' &&\n    coll.createIndex !== BaseCollection.prototype.createIndex;\n}","tryCatchPattern":"try {\n  await Model.collection.ensureIndex(spec);\n} catch (err) {\n  if (err.message === 'Collection#ensureIndex unimplemented by driver') {\n    await Model.collection.createIndex(spec); // legacy -> modern API\n  } else {\n    throw err;\n  }\n}","preventionTips":["Use Model.syncIndexes()/createIndexes() instead of touching Model.collection","In tests, stub the modern method set or use mongodb-memory-server","Ban legacy ensureIndex/findAndModify calls in lint rules for new code"],"tags":["mongoose","driver","index","legacy-api"],"backgroundTag":"driver-method-unimplemented","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}