Automattic/mongoose · error · Error
Collection#ensureIndex unimplemented by driver
Error message
Collection#ensureIndex unimplemented by driver
What it means
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.
Source
Thrown at lib/collection.js:144
method[0].apply(this, method[1]);
} else {
this[method[0]].apply(this, method[1]);
}
}
this.queue = [];
const _this = this;
immediate(function() {
_this.emitter.emit('queue');
});
return this;
};
/**
* Abstract method that drivers must implement.
*/
Collection.prototype.ensureIndex = function() {
throw new Error('Collection#ensureIndex unimplemented by driver');
};
/**
* Abstract method that drivers must implement.
*/
Collection.prototype.createIndex = function() {
throw new Error('Collection#createIndex unimplemented by driver');
};
/**
* Abstract method that drivers must implement.
*/
Collection.prototype.findAndModify = function() {
throw new Error('Collection#findAndModify unimplemented by driver');
};
View on GitHub (pinned to 49cdab0136)
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
Example fix
// before
await Model.collection.ensureIndex({ email: 1 }, { unique: true });
// after
await Model.collection.createIndex({ email: 1 }, { unique: true });
// or declare it on the schema: new Schema({ email: { type: String, unique: true } }) Defensive patterns
Strategy: type-guard
Validate before calling
const BaseCollection = require('mongoose/lib/collection');
function collectionSupports(coll, method) {
return typeof coll[method] === 'function' &&
coll[method] !== BaseCollection.prototype[method];
}
if (!collectionSupports(Model.collection, 'ensureIndex')) {
throw new Error('Driver does not implement ensureIndex; use createIndex');
} Type guard
const BaseCollection = require('mongoose/lib/collection');
function hasRealCreateIndex(coll) {
return typeof coll.createIndex === 'function' &&
coll.createIndex !== BaseCollection.prototype.createIndex;
} Try / catch
try {
await Model.collection.ensureIndex(spec);
} catch (err) {
if (err.message === 'Collection#ensureIndex unimplemented by driver') {
await Model.collection.createIndex(spec); // legacy -> modern API
} else {
throw err;
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Collection#createIndex unimplemented by driver
- Collection#findAndModify unimplemented by driver
- Path "${this.path}" may not have `index` set to false and `u
- Path "${this.path}" may not have `index` set to false and `s
- Path "${this.path}" may not have `index` set to false and `t
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/562a4c6470b57b92.
Report an issue: GitHub.