{"record":{"id":"0608f29617cf5536","repo":"Automattic/mongoose","slug":"collection-findandmodify-unimplemented-by-driver","errorCode":null,"errorMessage":"Collection#findAndModify unimplemented by driver","messagePattern":"Collection#findAndModify unimplemented by driver","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/collection.js","lineNumber":160,"sourceCode":"\nCollection.prototype.ensureIndex = function() {\n  throw new Error('Collection#ensureIndex unimplemented by driver');\n};\n\n/**\n * Abstract method that drivers must implement.\n */\n\nCollection.prototype.createIndex = function() {\n  throw new Error('Collection#createIndex unimplemented by driver');\n};\n\n/**\n * Abstract method that drivers must implement.\n */\n\nCollection.prototype.findAndModify = function() {\n  throw new Error('Collection#findAndModify unimplemented by driver');\n};\n\n/**\n * Abstract method that drivers must implement.\n */\n\nCollection.prototype.findOneAndUpdate = function() {\n  throw new Error('Collection#findOneAndUpdate unimplemented by driver');\n};\n\n/**\n * Abstract method that drivers must implement.\n */\n\nCollection.prototype.findOneAndDelete = function() {\n  throw new Error('Collection#findOneAndDelete unimplemented by driver');\n};\n","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/collection.js#L142-L178","documentation":"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.","triggerScenarios":"Model.collection.findAndModify(query, sort, update, opts); plugins or old libraries that still call the legacy method; test doubles implementing only modern methods.","commonSituations":"Codebases migrated from mongoose 2.x/3.x; dependencies written against the legacy driver API; mocks/stubs in unit tests missing findAndModify.","solutions":["Replace with Model.findOneAndUpdate()/Model.findOneAndDelete() or collection.findOneAndUpdate()","If a dependency requires it, implement findAndModify on your Collection subclass by delegating to findOneAndUpdate semantics","Upgrade the dependency to a version using the modern API"],"exampleFix":"// before\nawait Model.collection.findAndModify({ name: 'x' }, [['name', 1]], { $set: { seen: true } }, { new: true });\n\n// after\nawait Model.findOneAndUpdate({ name: 'x' }, { $set: { seen: true } }, { new: true });","handlingStrategy":"type-guard","validationCode":"const BaseCollection = require('mongoose/lib/collection');\nfunction supportsFindAndModify(coll) {\n  return typeof coll.findAndModify === 'function' &&\n    coll.findAndModify !== BaseCollection.prototype.findAndModify;\n}\nif (!supportsFindAndModify(Model.collection)) {\n  throw new Error('Legacy findAndModify unavailable; use findOneAndUpdate');\n}","typeGuard":"const BaseCollection = require('mongoose/lib/collection');\nfunction supportsFindAndModify(coll) {\n  return typeof coll.findAndModify === 'function' &&\n    coll.findAndModify !== BaseCollection.prototype.findAndModify;\n}","tryCatchPattern":"try {\n  await Model.collection.findAndModify(q, sort, update, opts);\n} catch (err) {\n  if (err.message === 'Collection#findAndModify unimplemented by driver') {\n    return Model.findOneAndUpdate(q, update, opts);\n  }\n  throw err;\n}","preventionTips":["Use model-level findOneAndUpdate everywhere","Audit dependencies for legacy collection.findAndModify calls","Keep test doubles aligned with the methods your code actually calls"],"tags":["mongoose","driver","legacy-api","find-and-modify"],"backgroundTag":"driver-method-unimplemented","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}