20

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

3 Answers 3

57

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:Passwrod@clustor.mongodb.net/

and your DB name is:

testDB

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

mongoose.connect('mongodb+srv://userName:Passwrod@cluster.mongodb.net/', {dbName: 'testDB'});
5
  • 1
    Awesome! Thanks!
    – HatzavW
    Feb 22, 2018 at 0:15
  • I'm getting this error :/ events.js:160 throw er; // Unhandled 'error' event ^ Error: No valid replicaset instance servers found
    – TheMisir
    Aug 26, 2018 at 8:50
  • 1
    @MisirJafarov are you trying to connect to an atlas instance which have replicasets? Aug 26, 2018 at 15:02
  • @ChaiHalfon sorry but actually I don't know what's replicaset :/ I'm new to MongoDB.
    – TheMisir
    Aug 27, 2018 at 16:05
  • i fixed it by updating mongoose
    – TheMisir
    Aug 31, 2018 at 11:06
3

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));
2

Just a side note if you ever get an error connecting to the MongoDB Atlas make sure:

  1. You've whitlisted our IP address / all (0.0.0.0/0)
  2. Your credentials are right
  3. Firewall / anti-virus are disabled

And if all fails

  1. Make sure your DNS is a public one. Or maybe change it to Google's public DNS. Throwing a link for the same: https://www.youtube.com/watch?v=F9u5ULrRt3Y

Goodluck!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.