Getting Started with MongoDB
Tags: mongodb
Published: 2020-08-24 14:50:56

MongoDB is a NoSQL,  general purpose, document based, distributed database. Document based means it stores data in JSON-like documents. 
In this article we're gonna set-up a free tier MongoDB cluster on MongoDB cloud and connect with our NodeJS & Express application.


> Launching the MongoDB Cluster

So to get Started, let's go to the  [MongoDB HomePage](https://www.mongodb.com/) and click on Start Free button on the center of the screen. 
![mongodb00.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598278599974/z1M2Gni7Q.jpeg)
Once, You click on Start Free, You'll be presented with a form similar to this: 
![mongodb0.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598278672546/0lmaOj4dG.jpeg)
Fill up the required fields and provide a valid Email Address and click on "Get Started Free". Afterwards you'll be logged into the Mongo Cloud Console and will be prompted to Create a Cluster. It will be something similar to :
![mongodb2.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598278844305/Gx5Ybp7Ft.jpeg)
Now, just rename your cluster and and click on "Create Cluster". This will kick the cluster launch process. After 5 minutes or so the cluster will be ready and you'll be allowed to create User.

> Creating DB User

![mongodb3.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598278950620/VNkZ6QZ2g.jpeg)
Now, from the left nav menu, click on "Database Access".  You'll have something similar to this
![mongodb4.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598279066618/Mgwi3lBfz.jpeg)
Now, click on "Add New Database User". You'll be prompted with something similar to 

![mongodb5.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598279122214/yAy-ld8dn.jpeg)
Enter the username and password in the respective field and click on Add User.


> WhiteListing IP Address

Once the Database User has been created, You'll need to Whitelist the IP address through which DB can be accessed. To do so, Go back to Clusters from the Left Nav-bar and click on CONNECT in the SANDBOX area.
![mongodb6.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598279287163/y6UFAH5Mt.jpeg)
Now, choose Allow Access From Anywhere and save. Then click on Choose a Connection Method.


> Getting Connection String

Now, after clicking on Choose Connection Method, You'll be presented with following options 
![mongodb7.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598279432694/ihmjJkHke.jpeg)

Choose "Connect Your Application" and you'll be provided with following 
![mongodbc.JPG](https://cdn.hashnode.com/res/hashnode/image/upload/v1598279688697/4mvUwS1jh.jpeg)
Copy the connection String by clicking on Copy and paste it on a NotePad file. It would look something like :

```
mongodb+srv://dmr:<password>@mytestcluster.f5kbx.mongodb.net/<dbname>?retryWrites=true&w=majority
``` 
Replace the <password> with the password you used when creating the user and replace <dbname> with anything you'd like. In my case i'd use testdb.


> Connecting with Our Express Application

Now, with our connection string, we can directly make the connection with our DB using **MongoDB Compass** but We'd like to connect our Express application with MongoDB. So create a folder called MongoTest and via terminal navigate to the folder.

Considering You're in the Mongotest folder, type the following commands:


```
npm init -y
npm i express mongoose --save
touch index.js
``` 
We're initializing package.json file and installing Express and Mongoose packages.
Now, lets write our Index.js file. It would be something similar to 


```
const express = require("express");
const mongoose = require("mongoose");

const app = express();

let db =
  "mongodb+srv://dmr:<Password>@mytestcluster.f5kbx.mongodb.net/testdb?retryWrites=true&w=majority";

mongoose
  .connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => console.log("mongoDB connected Successfully"))
  .catch((err) => console.log(err));

app.listen(3000, console.log("Server running on port 3000"));

``` 
Remember to replace the <Password> section with the Password you set for the user. Now, just type 

```
node index.js
``` 
If you've done everything correctly, You'll see the Prompt MongoDB Connected Successfully.

> Thanks for reading. In the upcoming Article, i'll be performing MongoDB CRUD operations too using NodeJS, Express and mongoose.