connection of deno to mongodb fails
Asked Answered
G

4

8

I'm trying to connect my deno application to mongodb but I get error.

import {MongoClient} from "https://deno.land/x/[email protected]/mod.ts";

const client = await new MongoClient();
await client.connect("mongodb+srv://deno:[email protected]/deno?retryWrites=true&w=majority");

const db = client.database("notes");

export default db;

everything seem to be fine but when I run the app, I get this error.

error: Uncaught (in promise) Error: MongoError: "Connection failed: failed to lookup address information: nodename nor servname provided, or not known"
                throw new MongoError(`Connection failed: ${e.message || e}`);
              ^
    at MongoClient.connect (client.ts:93:15)
    at async mongodb.ts:4:1
Gemmell answered 5/2, 2021 at 15:27 Comment(5)
Double check the <username> and <password>.Antananarivo
failed to lookup address information: - I don't think it's a problem with your code or Deno, but just an unreachable address. Try with a local db.Charlot
Have the same issueAlejandrinaalejandro
I have the same issueTorticollis
looks like problem with URL.Macy
P
5

2 problems that I see:

  • The code snippet above only works with Mongo installed in local machine.
  • The connection string use DNS Seed List, but the current library couldn't resolve to a list of hosts

To make it works with Mongo Atlas, you need to call a connect method with difference parameters and find a correct (static) host instead a (dynamic) DNS Seed List:

const client = new MongoClient();

const db = await client.connect({
  db: '<your db or collection with work with>',
  tls: true,
  servers: [
    {
      host: '<correct host - the way to get the host - see bellow>',
      port: 27017,
    },
  ],
  credential: {
    username: '<your username>',
    password: '<your password>',
    mechanism: 'SCRAM-SHA-1',
  },
});

How to get the correct host:

  • Open your Cluster in Mongo Atlas
  • Select Connect button
  • Select Connect to application option
  • Select Driver: Node.js and Version: 2.2.12 or later
  • Here you will see a list of host follow @ character
Pulido answered 12/4, 2021 at 15:19 Comment(0)
L
3

Wow that was a pain in the ass for me, but I could handle that adding authMechanism=SCRAM-SHA-1 and removing retryWrites=true&w=majority of the URI. So what you want is:

await client.connect("mongodb+srv://deno:[email protected]/deno?authMechanism=SCRAM-SHA-1");

It's been a while I hoped you have solved it!

Lentha answered 19/8, 2023 at 0:29 Comment(0)
I
0

Thank you @nthung.vlvn for the clue. Indeed the host needs to be a primary shard. It fixed the lookup address information, but I had another error, that my credentials are incorrect. I had to add db "admin" to credential:

credential: {
    username: '<your username>',
    password: '<your password>',
    db: "admin",
    mechanism: 'SCRAM-SHA-1',
}

That is weird, because I do not have admin db in my Atlas. Anyway it started to work.

Instauration answered 13/9, 2021 at 8:55 Comment(0)
L
0

This solved my issue: I'm using Version [email protected]

Mongo module wraps Rust Mongo Package under the hood. So to solve this issue. It uses plugins systems, that's why you need --unstable flag when running code.

It also needs --allow-read, --allow-write and -allow-plugin permission.

final command would be: deno run --unstable --allow-read --allow-write --allow-plugin your_app.ts

Lounge answered 20/2, 2024 at 4:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.