DEV Community

Cover image for How to connect to MongoDB Atlas using Node.js

How to connect to MongoDB Atlas using Node.js

Rohit Dalal on July 23, 2020

In this post, I will tell you how you can connect to MongoDB Atlas, a cloud-based DB service, from your local Node.js / Express.js environment. Yo...
Collapse
 
ararsolatnera profile image
Shibiru A Ararso •

const mongoose = require("mongoose");

const url = "mongodb+srv://userName:@cluster0.xesyn.mongodb.net/DBname?retryWrites=true&w=majority";

mongoose.connect(url, (err) => {
if (err) throw err;
console.log("Database created!");
});
I Connect in this way

you explained in details thank you so much

Collapse
 
sdutta099 profile image
sdutta099 •

Is it a good practice to provide mongo admin password in app.js ? as if i push the code to git it might give warning . Btw i am new to node js. if not then what is best way to provide the password of admin in mongo url ?

Collapse
 
dalalrohit profile image
Rohit Dalal •

you can pass the credentials via environment variables and make sure you don't push that to git. something like is used in production

const url="..<process.env.USERNAME>&<process.env.PASSWORD>";
Enter fullscreen mode Exit fullscreen mode

added this just for demo purpose ;)

Collapse
 
sdutta099 profile image
sdutta099 •

thanks a lot for the reply yes i tried using above step only

Collapse
 
chinmay395 profile image
Chinmay-395 •

Thanks this helped.

Collapse
 
suyogk38 profile image
Suyog Kubde •

const express = require("express");
const app = express();
const { MongoClient } = require("mongodb");
const PORT = 4000;
let db;

let connectionString = mongodb+srv://suyogkubde02:9881815220@cluster0.i2ofrpx.mongod.../?retryWrites=true&w=majority;

// app.use(express.json());

MongoClient.connect(
mongodb+srv://suyogkubde
Uploading image:****@cluster0.i2ofrpx.mongodb.net/?retryWrites=true&w=majority,
{ useNewUrlParser: true },
(error, client) => {
console.log("error :", error);
console.log("client : ", client);
if (error) {
return console.log("Connection failed for some reason");
}
console.log("Connection established - All well");
db = client.db("crud");
app.listen(PORT, function () {
console.log("Server is running on Port: " + PORT);
});
}
);

For now I have just removed the password.
When I do node server.js it doesn't give any error or any output it just keep on loading even the execution don't end.

Collapse
 
mayankdev1822 profile image
Mayank-dev1822 •

Thanks a lot that helped 😄

Collapse
 
ivanzapatarivera profile image
Ivan J. Zapata-Rivera •

Hi, my coding friend!
I believe replacing
const url = ... with
const url = process.env.ATLAS_URI
would be a more secured approach.

Collapse
 
dalalrohit profile image
Rohit Dalal •

Yes, definitely my coding friend.
I've added , for demo purpose.

Collapse
 
sakibullah2006 profile image
Sakib •

right on point

Collapse
 
carnato profile image
Vishal Yadav •

thanks yar tune meri problem solve kr di!

Collapse
 
jhalmu profile image
Juha Halmu •

Ah... thanks. This was simple enought for me.

Collapse
 
shubhamsaroj23 profile image
shubhamsaroj23 •

i got this error

MongoParseError: option usecreateindex is not supported

later i came to know usecreateindex is know not supported in mongoos 6 so no need to add it