# Creating a MongoDB Database

### Creating a MongoDB Account

First, visit MongoDB's [website](https://www.mongodb.com/) to create a [Cluster](https://stackoverflow.com/questions/43445975/what-is-a-cluster-in-mongodb/43447558) that will be hosted on the cloud.

&#x20;&#x20;

![MongoDB main page](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJ3TTR6PNMPf8nOLS4a%2F-MJ3YfbKaVnOM9rul0Bo%2F1.PNG?alt=media\&token=3de4999e-560a-4a6b-abd5-a7a678fab2ef)

Click on **Start free** and you will be redirected to the registration form.

&#x20;

![MongoDB registration from](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJ3YhWlWIUJjO1VOhfX%2F-MJ3ZXUcJXcSfzQNsBMz%2F2.PNG?alt=media\&token=6736a0d7-a1d5-49af-9a44-8d53783cf907)

Once that is finished, you will be asked to enter your **organization's and project's names** (in this case I kept the default proposed names) and the **preferred language** (though this step is not required).

![Setting up the MongoDB account](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJ3ZbbLk5rIK4thxRvO%2F-MJ3a6RESF2lyLDEe03W%2F3.PNG?alt=media\&token=f7a046f4-0ada-4824-bc0f-a753e6f2afae)

Once you finished signing up, you will be redirected to the cluster creation page. Choose **Shared Clusters** as your plan (unless you feel fancy then you can choose a paid plan).

&#x20;

![Selecting the free plan](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJD3ecVSuXIeiBmbDf7%2F-MJD4FE3gI98cJz5PVWZ%2F4.PNG?alt=media\&token=6b0d9872-e99f-4d6c-91fb-802160cecda8)

### Creating a Cluster

In this step, you will choose a **cloud provider** (AWS, Google Cloud, or Azure) and the region depending on the provider, then click on the **Create Cluster** green button at the bottom right.

![Creating a starter cluster](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJD4iQCk8Vhdw2Sdu-U%2F-MJDKPJQL5vaMq7pi9Ko%2F5.PNG?alt=media\&token=c4e57eae-de92-49e1-bd56-03b6031ab30f)

If everything went well, you will be redirected to your dashboard, and after a few minutes (\~1-3min) your cluster should be ready.

![Waiting for the cluster to be created](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDPMmOrkADtwNmNZ4W%2F6.PNG?alt=media\&token=e1a87e4c-9aa0-4a19-8014-1f3661189ec8)

&#x20;

![Cluster successfully created](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDPVfOh7lykjk9T7nb%2F7.PNG?alt=media\&token=aef0dfed-5bff-4a24-a81e-235fb7259475)

### Retrieving the Connection String

The last step is to retrieve the connection string (i.e. database URI) that you will use to connect the backend to the database. Click on the **Connect** button under the cluster's name.

&#x20;

![Setting up the connection parameters](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDReue0f8yVf9w2HSb%2F8.PNG?alt=media\&token=c6cc2651-5563-455e-b32e-9e620883bae5)

Click on **Allow Access from Anywhere**, this will allow anyone to connect to your database given the string. The *0.0.0.0/0* IP address will be added to the whitelist. Don't forget to click on **Add Ip Address** to save.

&#x20;

![Successfully whitelisting all IP addresses](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDSdURoWD2Sl43xbaI%2F9.PNG?alt=media\&token=119d93d0-dd8c-49e9-a8d4-4cbecd179ef7)

Next, you will need to create a user that has permission to connect to the database. For security reasons, it is always recommended to use the auto-generated password. Once you are done, click on **Create Database User**

![Adding a database user](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDTNhaa8qbIq-9vczk%2F10.PNG?alt=media\&token=0b15d4cb-4b9b-4255-8b64-70967b2db5ab)

Then, click on **Choose a connection method**, and select **Connect your application**

![Choosing a connection method](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDUjA2mCWgJtbUOJ8H%2F11.PNG?alt=media\&token=4fccbbd7-fc4d-4688-834a-3e142f537b5a)

Select **Node.js** as the driver and 3.6 or **later** as the version.

![Specifying the connection driver and copying the database link](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDVqu0LchFXUKISIzU%2F12.png?alt=media\&token=d806ebbe-ad99-4d77-b63d-8e13926594af)

In the grey box, a string under the following format will be located.

```bash
mongodb+srv://<username>:<password>@[URL-STRING]/<dbname>?retryWrites=true&w=majority
```

Where **\<username>** is the database user's username (in this case it's *mootez*), **\<password> is the password** for the newly created user, and **\<dbname>** is the database name that we will create. You can copy the string and save it elsewhere and click on the **Close** button.

### Creating a Database

For the final part, we will create a database in our cluster.

Click on your cluster's name and navigate to the **Collections** tab. Click on **Add my Own Data** and specify a **name** and a **collection** for your database (collections are analogous to tables in relational databases, so a database can have multiple collections), in this case, I chose *smu* as a DB name and *users* as a first collection.

&#x20;

![Creating a new database](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDYqyG4iqVvuwcfdWk%2F14.PNG?alt=media\&token=5e78be99-e7ce-4c18-87ab-5d7f1742b54e)

&#x20;

![Database successfully created](https://3562566727-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MJ2dzCH38HoVBUGfU0z%2F-MJDKfiC_B69bRWsI9y-%2F-MJDZ-XNrKL-4s2VOxxm%2F15.PNG?alt=media\&token=e9fe8ae7-6a18-42e3-bd4e-34ffe40a5a02)

Don't forget to replace **\<dbname>** in the connection string with the database's name that you have chosen (it is *smu* in this case).

### Conclusion

You have now successfully created a MongoDB database. In the following section, we will show how to connect the backend code to communicate with the database.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://smu-portal.gitbook.io/get-started/backend/creating-a-mongodb-database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
