Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Connect mongodb compass with node.js

I’m trying to connect a nodejs with mongodb using mongo db compass and im getting the following error:

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the
database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (C:\Users\marin\Downloads\Project part2\Project part2\docker_app\node_modules\mongoose\lib\connection.js:797:32)
at C:\Users\marin\Downloads\Project part2\Project part2\docker_app\node_modules\mongoose\lib\index.js:332:10
at C:\Users\marin\Downloads\Project part2\Project part2\docker_app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise ()
at promiseOrCallback (C:\Users\marin\Downloads\Project part2\Project part2\docker_app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (C:\Users\marin\Downloads\Project part2\Project part2\docker_app\node_modules\mongoose\lib\index.js:1153:10)
at Mongoose.connect (C:\Users\marin\Downloads\Project part2\Project part2\docker_app\node_modules\mongoose\lib\index.js:331:20)
at connectDb (C:\Users\marin\Downloads\Project part2\Project part2\docker_app\src\connection.js:9:6)
at Server. (C:\Users\marin\Downloads\Project part2\Project part2\docker_app\server.js:27:3)
at Object.onceWrapper (events.js:519:28) {
reason: TopologyDescription {
type: ‘Unknown’,
servers: Map(1) {
‘cluster0.huaic.mongodb.net:27017’ => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}

My code is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const mongoose = require("mongoose");
const User = require("./User.model");

const connection = '"mongodb://cluster0.huaic.mongodb.net/myFirstDatabase"';

const connectDb = () => {
  return mongoose
    .connect(connection)
    .then((res) => {
      return res;
    })
    .catch((error) => {
      console.log(error);
    });
};

module.exports = connectDb;

I’ve allowed access from every IP on mongodb atlas and when I try to connect directly to atlas everything works fine.

>Solution :

Your connection string should be like so :

const connection = "mongodb+srv://yourusername:youruserpassword@cluster0.huaic.mongodb.net/yourdatabasename?retryWrites=true&w=majority"

And you set up a user that have that password and that username in this section in your atlas account :

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading