Connection via node/express gets closed / times out

Recently upgraded a server-side app to
node v14.4.0
MongoDB 4.2.6 Community

All recent packages
{ …
“express”: “^4.17.1”,
“mongodb”: “^3.5.9”,
“mongoose”: “^5.9.20”,

Simple setup:

const gooseoptions = {
  tls: true,
  tlsCAFile: '',
  sslValidate: true,
  useNewUrlParser: true,
  useUnifiedTopology: true

When I run the app, it seems to get a connection - but then after about 30 seconds it gets kicked out with this…

throw error;

MongoServerSelectionError: connection <monitor> to <my mongodb IP> closed
at Timeout._onTimeout (/home/ec2-user/myapp/**node_modules/mongodb/lib/core/sdam/topology.js:430:30**)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) {
    reason: TopologyDescription {
        type: 'ReplicaSetNoPrimary',
        setName: null,
        maxSetVersion: null,
        maxElectionId: null,
        servers: Map(2) {
            ' db...' => [ServerDescription],
            ' db...' => [ServerDescription]
        stale: false,
        compatible: true,
        compatibilityError: null,
        logicalSessionTimeoutMinutes: null,
        heartbeatFrequencyMS: 10000,
        localThresholdMS: 15,
        commonWireVersion: null

Note: the same error is posted on forum here on Jun 29, 2020:


A driver will try for 30 seconds to connect to the server using the Server Selection Algorithm. From your description, it seems that the driver cannot find a suitable server to connect to, for some reason.

The specific reason is printed in the error message: ReplicaSetNoPrimary. Meaning that the driver cannot seem to find/connect to the primary node. However, there could be many different situation that lead to this error.

From the tlsCAFile option you provided to the driver, I’m guessing that this is not an Atlas deployment which requires SSL to connect. For this experiment, have you tried connecting to the server without using SSL to ensure that the driver can reach the server? Have you tried connecting to the server using the mongo shell?

If you can connect without SSL but cannot with SSL, what is the relevant error message printed by the node driver and the mongod process? Typically the error message points to the exact problem.

Best regards,

Hi Kevin,

The deployment is on IBM Cloud using their “Databases for MongoDB” service.
Yes, I am able to connect using the Mongo shell perfectly fine.

Is there a way to see the connection attempt from within Mongo shell?

I have not tried connecting without SSL from the app.
Thank you for the guidance, I will follow up with my results…

Hi Hankins,

Is there a way to see the connection attempt from within Mongo shell?

Unfortunately no. This would have to be seen from the mongod logs.

Note that there seems to be an ongoing infrastructure issue with IBM Cloud, as there was a report of intermittent failures in NODE-2513. You might want to follow up with them as well if your experiments prove unsuccessful.

Best regards,