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

  1. Call createIndex instead -- ensureIndex is the pre-3.0 MongoDB API name
  2. Prefer schema-level indexes (field: { index: true }) or Model.syncIndexes()/Model.createIndexes()
  3. 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

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


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