{"record":{"id":"32d0e0712eb04186","repo":"Automattic/mongoose","slug":"no-connections-to-database-name-found","errorCode":null,"errorMessage":"No connections to database \"${name}\" found","messagePattern":"No connections to database \"(.+?)\" found","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"warning","filePath":"lib/drivers/node-mongodb-native/connection.js","lineNumber":169,"sourceCode":" *     // Connect to `initialdb` first\n *     const conn = await mongoose.createConnection('mongodb://127.0.0.1:27017/initialdb').asPromise();\n *\n *     // Creates an un-cached connection to `mydb`\n *     const db = conn.useDb('mydb');\n *\n *     // Closes `db`, and removes `db` from `conn.relatedDbs` and `conn.otherDbs`\n *     await conn.removeDb('mydb');\n *\n * @method removeDb\n * @memberOf Connection\n * @param {string} name The database name\n * @return {Connection} this\n */\n\nNativeConnection.prototype.removeDb = function removeDb(name) {\n  const dbs = this.otherDbs.filter(db => db.name === name);\n  if (!dbs.length) {\n    throw new MongooseError(`No connections to database \"${name}\" found`);\n  }\n\n  for (const db of dbs) {\n    db._closeCalled = true;\n    db._destroyCalled = true;\n    db._readyState = STATES.disconnected;\n    db.$wasForceClosed = true;\n  }\n  delete this.relatedDbs[name];\n  this.otherDbs = this.otherDbs.filter(db => db.name !== name);\n};\n\n/**\n * Closes the connection\n *\n * @param {boolean} [force]\n * @return {Connection} this\n * @api private","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/drivers/node-mongodb-native/connection.js#L151-L187","documentation":"Connection.prototype.removeDb(name) closes and detaches databases previously created on that connection via useDb(). It throws a MongooseError when no tracked db with that name exists in conn.otherDbs - the name was never created with useDb() on this connection, was already removed, or belongs to a different connection object.","triggerScenarios":"conn.removeDb('tenantDb') where 'tenantDb' was created via a different connection (e.g. mongoose.connection vs a createConnection() instance); calling removeDb twice for the same name; a typo in the database name.","commonSituations":"Multi-tenant code that creates per-tenant dbs with useDb() and cleans them up on logout; shutdown cleanup racing another cleanup that already removed the db; tests iterating over created dbs.","solutions":["Check existence first: if (conn.relatedDbs['tenant42']) conn.removeDb('tenant42') - relatedDbs keys are the db names.","Ensure the db was created from the same connection instance: const db = conn.useDb(name).","Track removed tenants in your lifecycle code to avoid double removal."],"exampleFix":"// before\nconn.removeDb('tenant42'); // throws if not created on this connection\n\n// after\nif (conn.relatedDbs['tenant42'] != null) {\n  conn.removeDb('tenant42');\n}","handlingStrategy":"validation","validationCode":"function removeDbSafe(conn, name) {\n  if (!Object.prototype.hasOwnProperty.call(conn.relatedDbs, name)) {\n    return false; // nothing to remove\n  }\n  conn.removeDb(name);\n  return true;\n}","typeGuard":"function hasDb(conn, name) {\n  return conn.relatedDbs[name] != null;\n}","tryCatchPattern":null,"preventionTips":["Create and remove tenant dbs through one helper that owns the conn.relatedDbs bookkeeping.","Never mix the default mongoose.connection with separate createConnection() handles for the same tenant db.","Treat removeDb() as check-then-remove, not idempotent-by-default."],"tags":["mongoose","multi-tenant","usedb","removedb","connection"],"backgroundTag":"unknown-database-reference","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}