Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail to connect Mongoose to Atlas

Tags:

I'm always connecting to the "admin" DB, which is a fixed bug.

Using Mongoose 5.0.6 MongoDb 3.6 and trying to connect to Atlas.

  1. My question, what driver Mongoose 5.0.6 depend on?
  2. How can I find out when Mongoose will have that fix?
  3. On a different direction, is there a way to connect with MongoDB then use this connection with Mongoose?

Cheers

like image 975
HatzavW Avatar asked Feb 22 '18 00:02

HatzavW


People also ask

Why is MongoDB not connecting to Atlas?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.


2 Answers

Basically you should try connecting with your url link, and specify the DB name on the mongoose connect method so if your cluster link is:

mongodb+srv://userName:[email protected]/

and your DB name is:

testDB

then you should call the mongoose.connect method as follows:

mongoose.connect('mongodb+srv://userName:[email protected]/', {dbName: 'testDB'});
like image 179
Chai Halfon Avatar answered Oct 01 '22 09:10

Chai Halfon


Connection is established when you use this connection string for (MongoShell 3.6+) :

var connDB = "mongodb+srv://<username>:<password>@cluster-fax0w.mongodb.net/test"

However, you will not be able to read/write data without entering the DBName in the mongoose.connect().

mongoose.connect(uri, { dbName: <your DB name> })
  .then( () => {
    console.log('Connection to the Atlas Cluster is successful!')
  })
  .catch( (err) => console.error(err));
like image 38
Shubham Upadhyay Avatar answered Oct 01 '22 11:10

Shubham Upadhyay