Slow connection to the server using NodeJS driver

Hello mongo gurus,

I recently signed up for the mongo atlas to eliminate some of the operational challenges of my own hosted/self managed community edition.

My app seems operational after changing the driver and connection string in uri (mongo+srv) format.

However, it takes close to 25 seconds to establish the initial connection. **That's really slow.**

Any help to improve the performance of the initial connection would be greatly appreciated.

Here my stack.

Node.js - v10.18.1

driver - “mongodb”: "^3.6

connection code

const MongoClient = require('mongodb').MongoClient;
url =  `mongodb+srv://${process.env.mongo_user}:${process.env.mongo_password}@${process.env.mongo_host}/${process.env.mongo_db}?retryWrites=true&w=majority`

 MongoClient.connect(url,
    {
      useUnifiedTopology: true,
      useNewUrlParser: true
    }).then((client) => {

      console.log("mongo db conection success");
    
    }).catch(err => {
      console.log("connection failure.... ", url);
      console.log("connection errored ", err);
    })

thank you,
Jag

Hi Jag,

I am having the same issue. However for me this started happening suddenly with no clear explanation. I am using nodemon for hot reloading. For months I have been working on an application that would connect to MongoDB using mongoose and every time there was a reload it would reconnect to Mongo within milliseconds. Now it has consistently been taking 20+ seconds to connect on every reload.

I cannot figure out what caused this to start happening and how to fix it. If anyone has any advice, it would be greatly appreciated.

Same here, connection to database is very slow and await doc.save() is not finishing

Can you test the connection with Compass and the shell to see if there are similar slow downs to the Node.js driver? This will eliminate the driver as the point of failure?

I’m having the same issue. I have 2 pc, on the first one the connection take less than 1s, one the 2nd one, the connection take 20s … they are connected to the same network and using the same version of node. 2 days ago I had no issue.
I have try to connect with mongoDB compass on windows 10 after I enter the url, the app is loading forever … (I’ve try to connect several times)

Hi all,

Hopefully this will help:

Thanks, indeed using the url given when using the mongo shell seems to work.

Try rebooting your router. This is what fixed the issue for me.

When using SRV the driver initiates a lookup that resolves a single hostname to the actual names of the hosts, this is the step that was causing the issue for me. I still do not know why my router was interfering with the SRV lookup specifically - my internet connection was otherwise perfect. I would love if someone could elaborate on why rebooting my router resolved this issue?

1 Like