Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Auto Reconnect with Unified Topology

After setting useUnifiedTopology=true, the Auto Reconnect stopped working and generates the following ERRORS:

DeprecationWarning: The option `reconnectInterval` is incompatible with the unified topology
DeprecationWarning: The option `reconnectTries` is incompatible with the unified topology
DeprecationWarning: The option `autoReconnect` is incompatible with the unified topology

How can i get the server to auto-reconnect with that new flag?

I'm using mongoose.createConnection to connect with the following options:

{
        autoReconnect: true,
        keepAliveInitialDelay: 300000,
        connectTimeoutMS: 300000,
        reconnectTries: Number.MAX_VALUE,
        reconnectInterval: 1000,
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
        poolSize: 10,
        auth: {
            authSource: "admin"
        },
        user: process.env.MONGO_USER,
        pass: process.env.MONGO_PASS
    }
like image 527
TBE Avatar asked Dec 24 '19 08:12

TBE


People also ask

How do you use useUnifiedTopology true?

How to use it. 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: const client = MongoClient('mongodb://localhost:27017', { useUnifiedTopology: true });

Why is useUnifiedTopology true?

useUnifiedTopology: False by default. Set to true to opt in to using the MongoDB driver's new connection management engine. You should set this option to true , except for the unlikely case that it prevents you from maintaining a stable connection.

What is use unified topology?

The unified topology is the first step in a paradigm shift away from a concept of “connecting” to a MongoDB deployment using a connect method.


1 Answers

According to the docs you shouldn't usually set autoReconnect in combination with useUnifiedTopology Source: https://mongoosejs.com/docs/connections.html#options

autoReconnect - The underlying MongoDB driver will automatically try to reconnect when it loses connection to MongoDB. Unless you are an extremely advanced user that wants to manage their own connection pool, do not set this option to false.

like image 96
Marc Borni Avatar answered Sep 19 '22 18:09

Marc Borni