mongo: Couldn’t connect with atlas database

when I tried to connect to an atlas database using:

import { MongoClient } from "https://deno.land/x/mongo@v0.21.0/mod.ts";
const client = new MongoClient();
await client.connect(
	"mongodb+srv://Eyoatam:" +
		Deno.env.get("MONGO_ATLAS_PASSWORD") +
		"@cluster0.xrb4d.mongodb.net/<dbname>?retryWrites=true&w=majority"
);

It threw the following error:

error: Uncaught (in promise) Error: failed to lookup address information: nodename nor servname provided, or not known
    at processResponse (deno:core/core.js:223:11)
    at Object.jsonOpAsync (deno:core/core.js:240:12)
    at async Object.connect (deno:cli/rt/30_net.js:224:13)
    at async MongoClient.connect (client.ts:41:14)
    at async app.ts:4:1

is there another way to connect with mongodb atlas?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 23 (7 by maintainers)

Most upvoted comments

Hi everyone, I was trying to connect to the Atlas database. I have tried all the possible solutions above. finally, I can connect with this way

await client.connect({
    db:'<db>',
    tls:true,
    servers: [
        {
            host: 'cluster0-shard-00-02.soz4m.mongodb.net',
            port: 27017
        }
    ],
    credential: {
        username:"<username>",
        password: '<pwd>',
        db: '<db>',
        mechanism:'SCRAM-SHA-1'
    }
})

You should set your primary cluster address to host. After that, It should connect successfully. I think you are getting an error while finding objects.

Can you try this? It works for me.

interface Room {
  _id?: { $oid: string };
  name: string;
  owner_id: string;
}

const rooms = db.collection<Room>('rooms');
const myRoom = rooms.find({}, {
      noCursorTimeout: false,
} as any);