Connecting to MongoDB with Mongoose
Tags: javascript, node, webdev, mongodb
Published: 2021-07-16 14:07:43

During the starting days of my journey into back-end programming, I had taken too much information right in the beginning. Although it's good to have plenty of resources but at times too much information right in the beginning this can be overwhelming ! 

Especially while connecting my app to mongoDB, I used to look out for pieces that would give me an overview of how it all inter connected behind the scenes. I did find some amazing pieces on many websites, but not exactly how I wanted it, hence after finally learning it I decided to write my own.

Enough with the small talks, Let's express our way into mongoDB on a mongoose ! 

# Setting up your mongoDB Atlas account :

* Step 1: Sign up with your google account on  https://www.mongodb.com/cloud/atlas :

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rabgxjray2p8vb4luif5.png)


* Step 2: Set up your account with the basic details:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88hr3oq9usk86no6rjf5.png)

* Step 3: Select the "Free Database" , this is enough for your basic projects or even a company in the start up phase.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5ffqimbp9yq5919afzq.png)

* Step 4: Create a cluster selecting your nearest tier, and keep the other settings as they are.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2yuxl8ztaycbuzdx5kjn.png)

* Step 5: Create a local user to connect to the database, keep the credentials somewhere safe, as they will help you to connect you to connect to the database.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pxpfgknnwzq91tkh7pxp.png)

* Step 6: Wait for the cluster to be created

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hf70sho4z3lc3r5dxmpm.png)

* Step 7: Click on connect once the cluster is set up.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bksdft4e3v0hysnz2wa.png)

* Step 8: Connect using MongoDB Compass

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rf4h83ls1m408cgtku1.png)

* Step 9: Download the mongoDB Compass if you don't have it set up already and copy the connection string being shown in your account.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lerk5mw7kcvmblpfz7at.png) 

* Step 10: Copy the connection string into mongoDB Compass and replace the line with "<password>" with the password you created in Step 5 and click "Connect"

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mx1wxum36h2ob5t3yoqt.png)

* Step 11: Once you see this after clicking connect, your atlas cloud DB is now successfully set up!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7d6f1641ujan33vpuim2.png)


# Connecting to the DB using node

* Step 1: Open a new repl on replit.com with NodeJS

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk3x9hfhi9oetfk8n4qz.png)

* Step 2: Click on examples after your repl it created

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpfaso3sbpuyxy5i4b1x.png)

* Step 3: Select the example for express app from the fields.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spi8mutgpvkmna8jea8a.png)

* Step 4: The express app is now set up, let us now proceed with the connection to our database.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrmx8zk7336nkahxsmtw.png)

* Step 5: Add mongoose as dependency in your app from packages tab in the left:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x59wnfq4mdphnzfqg3uv.png)

* Step 6: Click the connect button to get the URI to connect to your database with your node app

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1uis9ujpg1yfqxpozbhe.png)

* Step 6: Select the URI from your database which is used to connect to the app

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nax8pgtwhzxw7h7d4jnh.png)

* Note : Make sure that connections are allowed from everywhere (all IPs), to do this go to the Network Access tab in the left and edit the settings.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1fsc771kfc3au36y1lo.png)

* Step 7: Use the following code to connect to your database:

```
mongoose.connect('<your-URI-here>', {useNewUrlParser: true, useUnifiedTopology: true}).then(()=>console.log("connected !!")).catch(error=>console.error("mongoose connection failed...", error))
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t6kvp4ul02edwxrud69.png)

You are now connected to your mongoDB Atlas database using mongoose from your node app !

You can refer to the following repl for the exact code :
https://replit.com/@ShasheeshPuroh1/mongoDB#index.js