'How to automatically reconnect to mongodb after connection loss?

I am trying to figure out how I can make my nodejs/express app to automatically reconnect to mongodb after a connection loss. I've noticed that my app loses the connection when I pause/stop my mongodb cluster and never recconnect even when I enable back the cluster. Therefore I need to manually restart my app in order to re-connect to mongodb. It's not ideal for a production environment.

After some researches, I've tried to setup my connection like this :

const options = {
            useNewUrlParser: true,
            useCreateIndex: true,
            useUnifiedTopology: true,
            useFindAndModify: false,
            autoIndex: true,
            autoReconnect:true,
            reconnectTries: Number.MAX_VALUE,
            reconnectInterval: 500,
            bufferMaxEntries: 0,
            connectTimeoutMS: 10000,
            socketTimeoutMS: 45000,
        }

  mongoose.connect(MONGO_URI, options);

But I get the following errors :

(node:21749) DeprecationWarning: The option `autoReconnect` is incompatible with the unified topology, please read more by visiting http://bit. ly/2D8WfT6
...
(node:21749) DeprecationWarning: The option `reconnectTries` is incompatible with the unified topology, please read more by visiting http://bit. ly/2D8WfT6
...
(node:21749) DeprecationWarning: The option `reconnectInterval` is incompatible with the unified topology, please read more by visiting http://bit. ly/2D8WfT6

Any suggestion ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source