You may have several options as follows
In Azure Functions, it's recommended to reuse client instances rather than creating a new one with each function invocation. You could create a single, static client that every function invocation can use. This also applies to Azure Cosmos DB clients, and you can follow the example provided by Microsoft's official documentation to achieve that.
You could create a Singleton MongoDB connection that is used across your application. You can check an example of how to achieve this in Node.js on StackOverflow.
It is also recommended to set server socket options to keep the connection alive. Moreover, you could ping the database at regular intervals to prevent the connection from going idle. This approach is especially useful if your application experiences long periods of inactivity. Here's an example of how to implement this in Node.js.