Mongoose doesn't reconnect after receiving specific mongo events
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.5.0
Node.js version
14.19.3
MongoDB server version
4.2.21
Description
Recently upgraded to Mongoose 6 from 5 and getting the following behavior:
I get the following 2 events from Mongo:
serverDescriptionChanged
with type of Unknown
topologyDescriptionChanged
with type of ReplicaSetNoPrimary
Looking at the code in mongoose, these 2 events fall on both setting the state to disconnected
and no reconnection is attempted:
https://github.com/Automattic/mongoose/blob/ba323df0b2c56ba76ff81bd67b4dbac5f6f78c01/lib/connection.js#L877-L915
Is there a reason why this case would require to manually initiate a reconnect?
Steps to Reproduce
- Start a mongoose connection
- Receive/trigger
serverDescriptionChanged
with type ofUnknown
andtopologyDescriptionChanged
with type ofReplicaSetNoPrimary
- Mongoose disconnects and no reconnect is attempted
Expected Behavior
A reconnection would be triggered
Issue Analytics
- State:
- Created a year ago
- Comments:6
Top Results From Across the Web
Mongoose Reconnect Event is Not getting fired after some time
I finally found the answer for this, We need to use the server options of mongodb-native driver options like this.
Read more >Mongoose not reconnecting when MongoDB driver does #6140
After a disconnect the self.isConnected() check never evaluates to true, therefore the reconnect event is never fired.
Read more >Mongoose.connect is not working - Drivers & ODMs - MongoDB
I am getting “no connection” in my console. I wrote this in other file and required it in my index.js file.Here is code...
Read more >Mongoose v6.8.2: Connecting to MongoDB
disconnected : Emitted when Mongoose lost connection to the MongoDB server. This event may be due to your code explicitly closing the connection,...
Read more >Express Tutorial Part 3: Using a Database (with Mongoose)
This tutorial provides a brief overview of some of the available options and then goes on to show in detail the particular mechanisms...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
After upgrading to mongoose 6 I see a recurring heartbeat failure and the aforementioned events (this does not happen with mongoose 5). Following these events and mongoose setting the disconnected state my queries start failing:
I start getting the following errors if buffering is enabled:
and a different error if it is not:
This issue was closed because it has been inactive for 19 days since being marked as stale.