Can't connect to remote mongo cluster but in Robo3T I'm able to connect

I have a remote mongodb database server that I want to connect to using the MongoClient (from mongodb package) class.
Now, the problem is I’m able to connect to the server using Robo3t but when I try to do the same with Node.js I get an error that says:
MongoServerSelectionError: getaddrinfo ENOTFOUND

How to resolve this?

2 Likes

A very common problem is not having the proper address in the connection string. Are you sure you are passing it? Many of the tools that you will use to connect elsewhere will have sane defaults set up, with in code you have to be very explicit : )
DO NOT SHARE your connection url.

I think so. I passed my connection string in this format.

mongodb://username:password@address:portnumber

Is this not right?

The connection string looks ok, granted you replaced the variables. Some solutions may require you to use mongodb+srv format though. My first solution would be to go to my online db settings and copy a connection string from there - many solutions offer that.

Second try comparing your connection setting in Robot3T vs what you have in the code. Is it using an ssh tunnel perhaps?

Where is it that you host the db?

Take a look here Mongodb or mongodb+srv - Drivers & ODMs - MongoDB Developer Community Forums

I don’t have the privilege to access database settings. All I’m given are address, portnumber, and credentials. I used that to connect in Robo3T. It works there.

I used the same info in Node, and it’s not working.

You can do the following things to narrow down the cause:

  • downgrade the mongodb package
  • run the code on a different device
  • run the mongodb locally

Thanks, downgrading the driver version worked.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.