{"record":{"id":"1d00d9c78ba78cad","repo":"Automattic/mongoose","slug":"cannot-modify-mongoose-driver-if-a-connection-is-a","errorCode":null,"errorMessage":"Cannot modify Mongoose driver if a connection is already open. Call `mongoose.disconnect()` before modifying the driver","messagePattern":"Cannot modify Mongoose driver if a connection is already open\\. Call `mongoose\\.disconnect\\(\\)` before modifying the driver","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/mongoose.js","lineNumber":170,"sourceCode":" * Mongoose-specific interface that defines functions like `find()`.\n *\n * @memberOf Mongoose\n * @method setDriver\n * @api public\n */\n\nMongoose.prototype.setDriver = function setDriver(driver) {\n  const _mongoose = this instanceof Mongoose ? this : mongoose;\n\n  if (_mongoose.__driver === driver) {\n    return _mongoose;\n  }\n\n  const openConnection = _mongoose.connections && _mongoose.connections.find(conn => conn.readyState !== STATES.disconnected);\n  if (openConnection) {\n    const msg = 'Cannot modify Mongoose driver if a connection is already open. ' +\n      'Call `mongoose.disconnect()` before modifying the driver';\n    throw new MongooseError(msg);\n  }\n  _mongoose.__driver = driver;\n\n  if (Array.isArray(driver.plugins)) {\n    for (const plugin of driver.plugins) {\n      if (typeof plugin === 'function') {\n        _mongoose.plugin(plugin);\n      }\n    }\n  }\n\n  if (driver.SchemaTypes != null) {\n    Object.assign(mongoose.Schema.Types, driver.SchemaTypes);\n  }\n\n  const Connection = driver.Connection;\n  const oldDefaultConnection = _mongoose.connections[0];\n  _mongoose.connections = [new Connection(_mongoose)];","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/mongoose.js#L152-L188","documentation":"Error \"Cannot modify Mongoose driver if a connection is already open. Call `mongoose.disconnect()` before modifying the driver\" thrown in Automattic/mongoose.","triggerScenarios":"Thrown at lib/mongoose.js:170 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}