Automattic/mongoose · error · Error
Collection#createIndex unimplemented by driver
Error message
Collection#createIndex unimplemented by driver
What it means
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.
Source
Thrown at lib/collection.js:152
_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');
};
/**
* Abstract method that drivers must implement.
*/
Collection.prototype.findOneAndUpdate = function() {
throw new Error('Collection#findOneAndUpdate unimplemented by driver');
};
View on GitHub (pinned to 49cdab0136)
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
Example fix
// before
await Model.collection.createIndex({ sku: 1 }, { unique: true }); // mock collection
// after
const schema = new Schema({ sku: { type: String, unique: true } });
await Model.syncIndexes(); Defensive patterns
Strategy: type-guard
Validate before calling
const BaseCollection = require('mongoose/lib/collection');
function assertCreateIndexAvailable(coll) {
if (coll.createIndex === BaseCollection.prototype.createIndex) {
throw new Error('createIndex not implemented by this collection/driver');
}
}
assertCreateIndexAvailable(Model.collection); Type guard
const BaseCollection = require('mongoose/lib/collection');
function isDriverBackedCollection(coll) {
return typeof coll.createIndex === 'function' &&
coll.createIndex !== BaseCollection.prototype.createIndex;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Collection#ensureIndex unimplemented by driver
- Invalid graphLookup() argument. Must be an object.
- Collection#findAndModify unimplemented by driver
- Can't call `openUri()` on an active connection with differen
- Path "${this.path}" may not have `index` set to false and `u
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/717d96845bbbc164.
Report an issue: GitHub.