Question

NodeJs MongoDB Connection Parameter Issue with Digital Ocean Managed MongoDb

Connected Tutorial
This question is a follow-up to this tutorial:

I went through the tutorial, from start to finish, have a Digital Ocean Managed Mongo DB created, and am beating my head against a wall trying to get Node to successfully connect to the database. This is my connection code (sensitive values have been obscured):

let cachedDb = null;

  async function connectMongo() {
    if (cachedDb) {
      return cachedDb;
    }
    try {
      const url = 'mongodb://doadmin:****************@db-mongodb-****-*****-********.mongo.ondigitalocean.com/admin?authSource=admin&replicaSet=db-mongodb-****-*****'
      const client = await mongodb.connect(
        process.env.DATABASE_URL, {
          useNewUrlParser: true, 
          useUnifiedTopology: true,
          // tls: true,
          tlsCAFile: './ca-certificate.crt'
        }
      );
      const db = client.db('nanocrowd');

      cachedDb = db;
      return db;
    } catch(err) {
      console.log(err);
      throw(err);
    }
  }

When the code attempts a connection, the following error response is returned:

stack: 'MongoServerSelectionError: getaddrinfo ENOTFOUND db-mongodb-****-*****-********.mongo.ondigitalocean.com\n    at Timeout._onTimeout (node_modules\\mongodb\\lib\\sdam\\topology.js:325:38)\n    at listOnTimeout (internal/timers.js:554:17)\n    at processTimers (internal/timers.js:497:7)'

I’m able to connect to the managed mongodb instance from my local development machine using two different clients: mongo, and Compass. Both connect using the identical parameters supplied to the NodeJS connection.

Does anyone have a working example that they’re willing to share?

Thanks in advance.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 19, 2021
Accepted Answer

Hi there,

I could suggest, trying to change the mongodb:// part to mongodb+srv://

Let me know how it goes. Regards, Bobby

Hello,

I have absolutely the same error. The suggested change by Bobby Iliev does not help. Any other suggestion?

Thanks.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.