Just as Jake Wilson said: You can set the connection to a variable then disconnect it when you are done:
let db;
mongoose.connect('mongodb://localhost:27017/somedb').then((dbConnection)=>{
db = dbConnection;
afterwards();
});
function afterwards(){
//do stuff
db.disconnect();
}
or if inside Async function:
(async ()=>{
const db = await mongoose.connect('mongodb://localhost:27017/somedb', { useMongoClient:
true })
//do stuff
db.disconnect()
})
otherwise when i was checking it in my environment it has an error.