如何使用统一拓扑设置自动重新连接

TBE*_*TBE 16 mongoose node.js node-mongodb-native

设置后useUnifiedTopology=true,自动重新连接停止工作并生成以下错误:

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
Run Code Online (Sandbox Code Playgroud)

我怎样才能让服务器自动重新连接那个新标志?

我正在使用mongoose.createConnection以下选项进行连接:

{
        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
    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*rni 5

根据文档,您通常不应autoReconnectuseUnifiedTopologySource结合设置:https : //mongoosejs.com/docs/connections.html#options

autoReconnect - 底层 MongoDB 驱动程序将在失去与 MongoDB 的连接时自动尝试重新连接。除非您是想要管理自己的连接池的非常高级的用户,否则不要将此选项设置为 false。

  • 这就是脑残啊 因此,现在成千上万的人将重新实现自己的重新连接逻辑并创建许多自定义的有缺陷的重新连接逻辑...... (12认同)