I’m setting up a simple example but the connection is still open even after i got the response
const options = {};
if (process.env.APPLICATION_MONGODB_AUTH_USERNAME && process.env.APPLICATION_MONGODB_AUTH_PASSWORD) {
options.auth = {
username : process.env.APPLICATION_MONGODB_AUTH_USERNAME,
password : process.env.APPLICATION_MONGODB_AUTH_PASSWORD,
}
options.authSource = process.env.APPLICATION_MONGODB_AUTH_SOURCE ? process.env.APPLICATION_MONGODB_AUTH_SOURCE : "admin";
}
const client = new MongoClient(process.env.APPLICATION_MONGODB_CONNECTION_URL, options);
return await client.connect().then(async () => {
const database = client.db(process.env.APPLICATION_MONGODB_SSO_DB);
const collection = database.collection('loginAttemptLog');
return collection.find({}).toArray();
}).finally(async () => {
// do i have to explicitly close it here every time??
await client.close();
console.log('MongoDB connection closed');
});```
am i doing something wrong? do i have to manually close the connection every time i do a query?