
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...
For further actions, you may consider blocking this person and/or reporting abuse
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
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 ?
you can pass the credentials via environment variables and make sure you don't push that to git. something like is used in production
added this just for demo purpose ;)
thanks a lot for the reply yes i tried using above step only
Thanks this helped.
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(
:****@cluster0.i2ofrpx.mongodb.net/?retryWrites=true&w=majority,
mongodb+srv://suyogkubde
{ 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.
Thanks a lot that helped 😄
Hi, my coding friend!
I believe replacing
const url =
...
withconst url = process.env.ATLAS_URI
would be a more secured approach.
Yes, definitely my coding friend.
I've added , for demo purpose.
right on point
thanks yar tune meri problem solve kr di!
Ah... thanks. This was simple enought for me.
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