Just wanted to share some stackover flows related to your question.
I am using Mongoose as an ORM for my communication with Mongodb Atlas. And I do not close the connection anywhere. I just open a connection in the node express server, and monitor the connection for disconnection/reconnects
In general, it is always a good practice to close the resources after they are used and before exiting the application. The MongoClient#close() method API documentation says:
Close the client, which will close all underlying cached resources, including, for example, sockets and background monitoring threads.
When you create a MongoClient instance you create a connection; actually a pool of connections (aka connection pool). By default, the pool has 100 connections (and this depends upon the driver). This is configurable within the Java code. This means, ideally, you should have one Mongo Client instance per JVM.
So, the application’s client uses a connection from the pool and returns it back to the connection pool after its usage. There is no need for closing the connection explicitly. This way the application avoids creating and closing connections (which is an expensive operation).
Of course, you should close the MongoClient instance, with the close() method (I had mentioned in my previous reply) at the closing of the application, to clear all the resources.
This note is about using the code for creating the MongoClient object and its close() method.
MongoClient extends an interface called as java.lang.AutoCloseable. When the object implementing this interface is used within a try-with-resources statement (as in code sample below) the object’s close() method is called automatically at the exit of the try block (releasing the associated resources).
A feature of using this try-with-resources statement is that, even when the code within the try block aborts abruptly (due to an exception), the close method is called automatically, and safely releasing the associated resources.