Automattic/mongoose · error · Error

Collection#findAndModify unimplemented by driver

Error message

Collection#findAndModify unimplemented by driver

What it means

findAndModify is the legacy MongoDB command wrapper, kept abstract on mongoose's base Collection; only drivers that implement it can serve calls. Mongoose's own findOneAndUpdate paths use driver-specific implementations, so this Error means code called the collection-level legacy method on an object without it.

Source

Thrown at lib/collection.js:160

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');
};

/**
 * Abstract method that drivers must implement.
 */

Collection.prototype.findOneAndDelete = function() {
  throw new Error('Collection#findOneAndDelete unimplemented by driver');
};

View on GitHub (pinned to 49cdab0136)

Solutions

  1. Replace with Model.findOneAndUpdate()/Model.findOneAndDelete() or collection.findOneAndUpdate()
  2. If a dependency requires it, implement findAndModify on your Collection subclass by delegating to findOneAndUpdate semantics
  3. Upgrade the dependency to a version using the modern API

Example fix

// before
await Model.collection.findAndModify({ name: 'x' }, [['name', 1]], { $set: { seen: true } }, { new: true });

// after
await Model.findOneAndUpdate({ name: 'x' }, { $set: { seen: true } }, { new: true });
Defensive patterns

Strategy: type-guard

Validate before calling

const BaseCollection = require('mongoose/lib/collection');
function supportsFindAndModify(coll) {
  return typeof coll.findAndModify === 'function' &&
    coll.findAndModify !== BaseCollection.prototype.findAndModify;
}
if (!supportsFindAndModify(Model.collection)) {
  throw new Error('Legacy findAndModify unavailable; use findOneAndUpdate');
}

Type guard

const BaseCollection = require('mongoose/lib/collection');
function supportsFindAndModify(coll) {
  return typeof coll.findAndModify === 'function' &&
    coll.findAndModify !== BaseCollection.prototype.findAndModify;
}

Try / catch

try {
  await Model.collection.findAndModify(q, sort, update, opts);
} catch (err) {
  if (err.message === 'Collection#findAndModify unimplemented by driver') {
    return Model.findOneAndUpdate(q, update, opts);
  }
  throw err;
}

Prevention

When it happens

Trigger: Model.collection.findAndModify(query, sort, update, opts); plugins or old libraries that still call the legacy method; test doubles implementing only modern methods.

Common situations: Codebases migrated from mongoose 2.x/3.x; dependencies written against the legacy driver API; mocks/stubs in unit tests missing findAndModify.

Related errors


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