FG
🗄️ DatabasesMongoDB

Mongoose not reconnecting when MongoDB driver does

Freshover 7 years ago
Mar 14, 20260 views
Confidence Score86%
86%

Problem

Do you want to request a feature or report a bug? bug What is the current behavior? When connected to a Mongos container that is restarted Mongoose never reconnects. I make sure to pass these options: [code block] However the logs show me that the driver does reconnect: [code block] If the current behavior is a bug, please provide the steps to reproduce. 1 Run a nodeJS app that connects to a Mongos on a Docker container 2 Restart your container What is the expected behavior? Mongoose should be reconnecting when the container comes back online. Please mention your node.js, mongoose and MongoDB version.* nodeJS: 8.9.4 and 9.2.0 mongoose: 5.0.5 mongodb: 3.4.10 EDIT: So I'm using useDb() to connect to other tenant's databases and I just noticed that after the Mongos restart all the calls made to the parent connection db on which I used useDb() works but the calls made on the children connections don't resolve. Also the readyState is never set to 1 on the parent connection even if the connection is properly reconnected.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Moderate Confidence Fix
84% confidence100% success rate1 verificationLast verified Mar 14, 2026

Solution: Mongoose not reconnecting when MongoDB driver does

Low Risk

I did some digging around in the drivers code and this is what I found: mongodb-core/lib/topologies/mongos.js [code block] After a disconnect the self.isConnected() check never evaluates to true, therefore the reconnect event is never fired.

84

Trust Score

1 verification

100% success
  1. 1

    I did some digging around in the drivers code and this is what I found:

    I did some digging around in the drivers code and this is what I found:

  2. 2

    mongodb-core/lib/topologies/mongos.js

    [code block]

  3. 3

    After a disconnect the self.isConnected() check never evaluates to true, therefo

    After a disconnect the self.isConnected() check never evaluates to true, therefore the reconnect event is never fired.

Validation

Resolved in Automattic/mongoose GitHub issue #6140. Community reactions: 0 upvotes.

Verification Summary

Worked: 1
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

mongoosemongodbodmcan't-reproduce