Creating a MongoDB Database

The first step is to create a MongoDB database. Since this project is about collaborative development, we will be hosting the database on the cloud rather than locally

Creating a MongoDB Account

First, visit MongoDB's website to create a Cluster that will be hosted on the cloud.

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

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).

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).

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.

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

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.

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.

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

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

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

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

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.

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.

Last updated