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

  1. Use Model.createIndexes() / Model.syncIndexes() or schema index definitions instead of Model.collection.createIndex()
  2. Ensure the model is connected to a real connection (await mongoose.connect) before touching Model.collection
  3. 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

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


AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21). Data as JSON: /api/errors/717d96845bbbc164. Report an issue: GitHub.