deno connect to mongodb

Viewed 1008

I've tried to follow this tutorial https://www.youtube.com/watch?v=VF38U2qd27Q, but to no avail. I realised that the syntax in the video already obsolete for example connectWithUri to become connect.

but when I tried to connect to mongo using deno_mongo with the latest docs, it still not working.

import { MongoClient } from "https://deno.land/x/mongo@v0.20.1/mod.ts";

const dbString = `mongodb://${mongoUser}:${mongoPass}@${mongoHost}:${mongoPort}`;
const client = new MongoClient();
client.connect(dbString);
const db = client.database(mongoDB)
this.users = db.collection<UserSchema>("users");

Then I found another library denodb but again can't connect to mongodb:

import { Database } from 'https://deno.land/x/denodb/mod.ts';

const dbString = `mongodb://${mongoUser}:${mongoPass}@${mongoHost}:${mongoPort}`;
this.db = new Database('mongo', {
  uri: dbString,
  database: mongoDB
});

the error message:

error: Uncaught AssertionError
deno        |     throw new AssertionError(msg);
deno        |           ^
deno        |     at assert (asserts.ts:152:11)
deno        |     at MongoClient.database (client.ts:48:5)
deno        |     at new connectDB (connectDB.ts:35:23)

which part is wrong?

4 Answers

Looking at the deno_mongo README on GitHub.

For a local database you should use

//Connecting to a Local Database
await client.connect("mongodb://localhost:27017");

And if you are connecting to Mongo Atlas Database (and probably any other remote database) you should use:

//Connecting to a Mongo Atlas Database
await client.connect({
  db: "<db_name>",
  tls: true,
  servers: [
    {
      host: "<db_cluster_url>",
      port: 27017,
    },
  ],
  credential: {
    username: "<username>",
    password: "<password>",
    db: "<db_name>",
    mechanism: "SCRAM-SHA-1",
  },
});

FYI

If you are using Mongo Atlas make sure to split up the connection string you get from 'Connection Wizard' in the Mongo Atlas dashboard over 3 (or how many replicas you have) entries in the server array. Like this:

servers: [
  {
    host: this.dbUrl1, // e.g. <name-of-cluster>-00-00.fbnrc.mongodb.net
    port: 27017,
  },
  {
    host: this.dbUrl2, // e.g. <name-of-cluster>-00-01.fbnrc.mongodb.net
    port: 27017,
  },
  {
    host: this.dbUrl3, // e.g. <name-of-cluster>-00-02.fbnrc.mongodb.net
    port: 27017,
  }
]

You get the connection string by:

  • Click 'Clusters' under Data storage (left side of the screen)
  • Click 'Connect' button
  • Click 'Connect to Application'
  • Select Driver: 'Node.js' and Version: '2.2.12 or later'

Connection string is displayed below. The servers are listed in a comma separated manner like this:

...<name-of-cluster>-00-00.fbnrc.mongodb.net:27017,<name-of-cluster>-00-01.fbnrc.mongodb.net:27017,<name-of-cluster>-00-02.fbnrc.mongodb.net:27017...

FYI-2

Make sure the master replica is listed first in the server array. Because if you want to do insertions into the database the master replica should be targeted. For me this was the 2nd mongo url, therefore the following server array worked for me:

servers: [
  {
    host: this.dbUrl2,
    port: 27017,
  },
  {
    host: this.dbUrl1,
    port: 27017,
  },
  {
    host: this.dbUrl3,
    port: 27017,
  }
]

the below code is working for me.


import { DataTypes, Database, Model } from 'https://deno.land/x/denodb/mod.ts';

const db = new Database('mongo', {
  host: 'mongodb://localhost:27017',
  username: '',
  password: '',
  database: 'DBMYAPP',
});
console.log(db)

I also faced the same issue while updating deno_mongo to the latest version. Use await to resolve client.connect method

Try this:

import { MongoClient } from "https://deno.land/x/mongo@v0.20.1/mod.ts";

const dbString = `mongodb://${mongoUser}:${mongoPass}@${mongoHost}:${mongoPort}`;
const client = new MongoClient();
await client.connect(dbString);
const db = client.database(mongoDB)
this.users = db.collection<UserSchema>("users");

I had the same problem on windows 10, so try this on your local mongodb:

await client.connect("mongodb://127.0.0.1:27017");
Related