Create MongoDB database and connect with Mongoose
Tags: node, javascript, beginners
Published: 2022-12-16 14:58:48

## Table Of Contents
  * [Create MongoDB Database](#mongo-db)
  * [Build a Database Cluster](#db-cluster)
  * [Connect with Mongoose](#connect-mongoose)

> *For this series, I'm following an excellent video tutorial from [Traversy Media](https://www.youtube.com/playlist?list=PLillGF-RfqbbQeVSccR9PGKHzPJSWqcsm)*

### Create MongoDB Database <a name="mongo-db"></a>
First, go to the [MongoDB](https://account.mongodb.com/account/login) site and signup. There is a signup from google or GitHub option as well.
After signup, create your organization to continue.
1. Click **Create an organization** button.
2. Type your organization name and select `MongoDB Atlas` and press next.
3. You don't need to add any members and permissions. Just click **Create organization** button. 

Now we need to create a project.
1. Click **New project** button.
2. Type name of project `todos` and click next.
3. And press **Create Project** button.

### Build a Database Cluster <a name="db-cluster"></a>
After creating a database, you can choose a cloud provider and other specs.
- Click the **Build a Database** button and choose the Shared (free) option. It is suitable for learning purposes and small projects.
- On the next page, you can select the cluster provider, region, and other settings. But I am keeping everything as default and only updating the name of the cluster.
- So I typed the "TodosCluster" name and pressed **Create Cluster** button.

![cluster-name](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1coxyedozyftrj3ho6o.png)

- Next, create a username and password to have read and write access to your database. Enter `username` and `password` and click **Create User** button.

![user-mongodb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhplfhnh9ujzclw4vak7.png)

- And after that, add your IP address to connect with your cluster in the local environment.

![ip-address](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsxpnpln9vc2jcmyex00.png)

- Finally, press the **Finish and Close** button.

Before connecting the database with our project, let's quickly create a todos collection.
1. Go to the `Collections` tab and press **Add my own data** button.
2. Type *todosapp* as `database name` and *todos* as `collection name` and press **Create**.

### Connect with Mongoose <a name="connect-mongoose"></a>
- Go to the overview tab and click the **connect** button.
- Select *Connect your application* option and copy the connection string.
- Go to the `.env` file and add the `MONOGO_URI` variable and paste the connection string there.
- Replace << password >> with your db password and type your db name after `mongodb.net/<project-name>?` in the connection string.
- Now create a new folder `config` and inside it a new file `db.js`. In this file, we'll write the code to connect with mongoose.

```javascript
const mongoose = require('mongoose');

const connectDB = async () => {
    try {
        const conn = await mongoose.connect(process.env.MONGO_URI);
        console.log(`Mongo db connected: ${conn.connection.host}`);
    } catch (error) {
        console.log(error);
        process.exit(1);
    }
};

module.exports = connectDB;
```
and add these lines to `server.js` file.
```javascript
const connectDB = require('./config/db');

connectDB();
const app = express();
...
```
Lets run the server, and you'll see that DB is connected.

![db-connected-terminal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yoco1finw5xq39a7yn14.png)

In the next blog, I'll create a todos model and update the controller methods.

Thank you for reading!
Feel free to connect on [Twitter](https://twitter.com/Aneeqa_Khan25)