Step 1 : SignUp and create a cluster
> Go to MongoDB Website and Create an account - https://www.mongodb.com/
> Create a Project
> Create a Cluster
- Shared
- Cloud Provider - AWS
- Region - anywhere you like
Step 2 : Create an Database
Now the cluster takes 2-3 minutes to create, so by that time let's setup our database
> Got to Database Access
> Add New Database User - setup id and password for the database
> click on create
Step 3: Create Network Access
> Choose "Allow access from anywhere"
> Click on create
Step 4 :
Now that our cluster is created,
>Go to Database > click on the "Connect" button
> Choose " connect our application"> Copy the " connection-string "
NOW GO BACK TO YOUR VS CODE EDITOR
Step 5 :
> go to mern-stack/backend
>Install Mongoose - npm i mongoose
> Open .env file
> Create a new variable and paste the "connection string" you just copied"
>.env file :
PORT = 7000
MONGO_URI = "mongodb+srv://aatu020:<password>@cluster0.t0wil.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
(Makes sure to replace <password> with your database password)
Step 6 : Creating a Database Connection
>Create a file name "config" inside backend
> Navigate to notes-saver/backend/config
>Create a java script file with the name "db.js' (any name is fine)
db.js file -
------------
>Go to server.js -
How do terminal looks -
And connection is successful.
Hurray! You have successfullyestablished the database connection(mongoDB) with backend (nodejs)
0 Comments