18

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
    }
TBE
  • 1,002
  • 1
  • 11
  • 32

1 Answers1

7

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.

Dee
  • 7,455
  • 6
  • 36
  • 70
Marc Borni
  • 384
  • 1
  • 17
  • I'm not using mongoose but having problem with the raw mongodb module, it doesn't allow me use autoReconnect=true: (node:4642) DeprecationWarning: The option `autoReconnect` is incompatible with the unified topology, please read more by visiting http://... (node:4642) DeprecationWarning: The option `reconnectTries` is incompatible with the unified topology, please read more by visiting http://... – Dee Jan 29 '20 at 10:23
  • Mongoose uses the mongodb nodejs driver underneath. So the same principles apply :) – Marc Borni Jan 29 '20 at 11:26
  • 1
    yeah but the new unified topo doesn't allow the old options 'autoReconnect' and 'reconnectTries' – Dee Jan 29 '20 at 14:46
  • 1
    That's exactly my point :) You shouldn't set these properties according to the docs. You should implement your own reconnect tries. See this library for an example: https://github.com/nestjs/mongoose/blob/master/lib/mongoose-core.module.ts#L62 – Marc Borni Jan 30 '20 at 08:49
  • 15
    This is braindead. So now tens of thousands will have reimplement own reconnection logic and create many custom buggy reconnection logics ... – RushPL Jul 23 '20 at 06:02
  • It seems that latest versions of MongoDB and moongoose drivers automatically try to reconnect when it loses connection to MongoDB so no need for the autoreconnect option – kimy82 Jun 18 '21 at 19:52