Adel Benyahia
Nov 14, 2022


According to mongo documents, you should not (usually) combine useUnifiedTopology with autoReconnect

We have two options;
1. useUnifiedTopology
The unified topology is available now behind the useUnifiedTopology feature flag. You can opt in to using it by passing the option to your MongoClient constructor:
mongoose.connect(uri', { useUnifiedTopology: true });

2. keepAlive
For long running applications, it is often prudent to enable keepAlive with a number of milliseconds. Without it, after some period of time you may start to see "connection closed" errors for what seems like no reason. If so, after reading this, you may decide to enable keepAlive:

mongoose.connect(uri, { keepAlive: true, keepAliveInitialDelay: 300000 });

The final connection eill be like this:
mongoose.connect(uri, {useUnifiedTopology: true, keepAlive: true, keepAliveInitialDelay: 300000 });



Adel Benyahia

Web application developer (HTML │ CSS │ JS | ReactJS | NextJS | NestJS | MERN)