Automattic/mongoose · warning · MongooseError
No connections to database "${name}" found
Error message
No connections to database "${name}" found What it means
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.
Source
Thrown at lib/drivers/node-mongodb-native/connection.js:169
* // Connect to `initialdb` first
* const conn = await mongoose.createConnection('mongodb://127.0.0.1:27017/initialdb').asPromise();
*
* // Creates an un-cached connection to `mydb`
* const db = conn.useDb('mydb');
*
* // Closes `db`, and removes `db` from `conn.relatedDbs` and `conn.otherDbs`
* await conn.removeDb('mydb');
*
* @method removeDb
* @memberOf Connection
* @param {string} name The database name
* @return {Connection} this
*/
NativeConnection.prototype.removeDb = function removeDb(name) {
const dbs = this.otherDbs.filter(db => db.name === name);
if (!dbs.length) {
throw new MongooseError(`No connections to database "${name}" found`);
}
for (const db of dbs) {
db._closeCalled = true;
db._destroyCalled = true;
db._readyState = STATES.disconnected;
db.$wasForceClosed = true;
}
delete this.relatedDbs[name];
this.otherDbs = this.otherDbs.filter(db => db.name !== name);
};
/**
* Closes the connection
*
* @param {boolean} [force]
* @return {Connection} this
* @api privateView on GitHub (pinned to 49cdab0136)
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.
Example fix
// before
conn.removeDb('tenant42'); // throws if not created on this connection
// after
if (conn.relatedDbs['tenant42'] != null) {
conn.removeDb('tenant42');
} Defensive patterns
Strategy: validation
Validate before calling
function removeDbSafe(conn, name) {
if (!Object.prototype.hasOwnProperty.call(conn.relatedDbs, name)) {
return false; // nothing to remove
}
conn.removeDb(name);
return true;
} Type guard
function hasDb(conn, name) {
return conn.relatedDbs[name] != null;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Connection#createClient not implemented by driver
- Cannot call `${this.name}.${i}()` before initial connection
- Connection has been closed and destroyed, and cannot be used
- `useConnection()` requires a Mongoose connection.
- The connection passed to `useConnection()` has a different v
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/32d0e0712eb04186.
Report an issue: GitHub.