Search code examples
mongoose

How to close all database connections - moongose


Seems when I connect to mongodb using connect api of mongoose, I get an array of connections within the connection object

const connection = await mongoose.connect(process.env.DATABASE_URL);
        var dbClient = connection.connections[0].client;

If I want to close the connection, do I need to go through each connection or is there a higher level api which could close all connections?


Solution

  • You can use : mongoose.disconnect();

    Mongoose reference : https://mongoosejs.com/docs/api/connection.html#Connection.prototype.close() Here is reference link : How to close mongodb connection using mongoose