Intermittent MongoServerSelectionError when connecting from NodeJS using Google Cloud Run + MongoDB Atlas

Hey folks,

Hi, I’m having problems with connecting to MongoDB Atlas from a Google Cloud Run instance. I’m intermittently (a few times per day) getting a MongoServerSelectionError when my server is initializing and trying to connect. This is my current setup:

  • Using M10 tier cluster on MongoDB 4.4.
  • Connecting using NodeJS driver version 3.6.0.
  • useUnifiedTopology set to true.
  • Whitelisted all IP addresses in MongoDB atlas for the cluster.

I’m not seeing any alerts in MongoDB atlas. Please note that this is an intermittent issue. Sometimes it connects sometimes it doesn’t. Because my app currently has very low traffic there is never more than one instance running so the 30-second timeout + time to restart of the container makes this problem critical.

Here’s the stack trace:

MongoServerSelectionError: connection timed out at Timeout._onTimeout (/usr/src/app/node_modules/mongodb/lib/core/sdam/topology.js:438: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 {
xxx-00-00-0qvnm.gcp.mongodb.net:27017’ => [ServerDescription],
xxx-00-01-0qvnm.gcp.mongodb.net:27017’ => [ServerDescription],
xxx-00-02-0qvnm.gcp.mongodb.net:27017’ => [ServerDescription]
},
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}

Can anybody assist with troubleshooting this?

Regards,
Stefan

2 Likes

Sorry I’m on MongoDB version 4.2 not 4.4

Has this issue been resolved for you? I’m having the same issue, of intermittent connectivity due to a timeout, but mine is happening from a DigitalOcean droplet. I’ve tried almost everything, except use Mongoose.

For my setup:

  • using MongoDB Atlas M0, 4.2.11
  • mongodb native driver 3.6.3

I resolved it by setting useUnifiedTopology: false, works great now.

5 Likes

I had a similar issue, however setting the useUnifiedTopology: false did not help me in my case.
Is there any other fixes to solve this error?

1 Like

Hi, I am new to MongoDB Atlas too. A few days ago, I had connection problem using Python. I have managed to find a solution for that.

Just now, using MongoDB Atlas NodeJs tutorial, using the same Atlas connection string in my Python stuff. I had the below timeout error.

It turned out, my Public IP address has changed because I reset my Windows 10 internet connection. I added the new Public IP to Atlast IP whitelist and it works.

This is my case. I am new to this, I hope this might help someone…


MongoServerSelectionError: connection to 13.55.193.163:27017 closed
at Timeout._onTimeout (D:\Codes\nodejs\mongodb\node_modules\mongodb\lib\sdam\topology.js:312:38)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7) {
reason: TopologyDescription {
type: ‘ReplicaSetNoPrimary’,
servers: Map(3) {
‘cluster0-shard-00-00 . 71o6u . mongodb . net : 27017’ => [ServerDescription],
cluster0-shard-00-01.71o6u.mongodb.net 27017’ => [ServerDescription],
cluster0-shard-00-02.71o6u.mongodb.net:27017’ => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: ‘atlas-vx7zhd-shard-0’,
logicalSessionTimeoutMinutes: undefined
}
}

2 Likes

This article helped me with this issue. Fixing MongoServerSelectionError while connecting MongoDB with node.js | by Barak Saidoff | Medium

1 Like

@Stefan_Li Did you fix this? I have the same problem and cannot find a way to resolve it

Same - I also keep getting the error intermittently when using Cloud Run with Atlas.

Hi, i have same issue, with NodeJs and Mongoose, on GCP App Engine with peer conection and using private url:

mongodb+srv://user:pass@xx-pri.qteth.mongodb.net/?retryWrites=true&w=majority

options: {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
  }

Node version: 14.17.4
Mongoose verison: 5.13.13

The problem happens intermittently, it does not follow a pattern, it has already happened twice in a day, but it has also been about a month without happening. Here is the error received from the application:

connection timed out MongoServerSelectionError: connection timed out
    at Timeout._onTimeout (/app/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)

I tried allow conection from 0.0.0.0/0 but dont work

2 Likes

Hi ,did you solve your problem? I had the same problem.