简体   繁体   中英

How detect MongoDB reconnection for replica set

If I try to register for the event 'reconnect' in a MongoDB replicaset:

db.on('reconnect', () => console.log('Reconnected'));

I receive a deprecation warning as:

DeprecationWarning: The `reconnect` event is no longer supported by the unified topology

How can I handle a case of lost MongoDB connection (all servers in the replica set) but I want to be notified of servers availability status (when at least one server become again available)?
Suppose to handle this in a Node app with MongoDB native drivers. Thanks in advance.

If we take a look at the spec regarding the unified topology , we can find the following section:

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. Consider for a moment what it means to be connected to a replica set: do we trigger this state when connected to a primary? A primary and one secondary? When connected to all known nodes? It's unclear whether its possible to answer this without introducing something like a ReadPreference parameter to the connect method. At this point “connecting” is just one half of “operation execution” - you pass a ReadPreference in, and await a selectable server for the operation, now we're connected!

There are, however, new events you can listen to and that might be useful for your usecase -> see this for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM