Skip to main content

In this section we will talk about our MongoDB DATABASE SETUP!! ✨

Since we're mastered setting our our Express backend, now let's try to save our data somewhere!

Create Account and Connect to MongoDB ✨

Let's learn how to first setup our MongoDB! (credits to Simon's notes but brought here for your convenience~)

1. Create FREE Atlas Account

(You can use Google Login)

2. Welcome to ATLAS

(Fill in anything you'd like but here's an example below) step 1

3. Create a Database

step 2

To note you can name the cluster whatever you'd like but remember to add it into your DATABASE_URL which we will introduce later into your .env file!

4. Create a Database User

This is so that you can access your database using the application you've created step 3

You will then see this page step 4

REMEMBER TO TAKE NOTE OF YOUR CREDENTIALS YA!! You will also need it in your DATABASE_URL

5. Allow your database to be accessed from any IP-Address (anywhereeee~~~)

If you don't do this you won't be able to connect!!

  • Scroll down to the second step: Where would you like to connect from?
  • In the IP Address field, enter the text 0.0.0.0/0 exactly as shown below.
  • In the description, write Anywhere. Then click the Add Entry button.

step 5

  • Finish and close

step 6

6. Get the CONNECTION STRING

(we will use this as a connection for our app to our database!)

  • click Connect button

step 7

  • select Drivers

step 8

  • copy the string and ADD IT INTO YOU .env file it will look like this
DATABASE_URL=<this-is-my-string-here-without-any-quotes>

❗️ please set up your .env file and gitignore - see .env & gitignore file notes

step 9

The string will look like this: mongodb+srv://<username>:<password>@<cluster-name>.uwdsn12.mongodb.net/<database-name>?retryWrites=true&w=majority

REPLACE <username>, <password> and <database-name> respectively, usually cluster-name would've been prefilled.

  • add in the exact database you'd like to connect to in the string!

IMPORTANT! add in the database string you want to connect to! The connection string does not include a database name by default.You'll add an appropriate database name between the /? part of the connection string for each project.

For example, if you have a project named mongoose-movies, you could give the database the same name: mongodb+srv://<username>:<password>@<cluster-name>.uwdsn12.mongodb.net/mongoose-movies?retryWrites=true&w=majority

View the data in my MongoDB Database!

So now that you've created a database, although you do not have any data in it right not (you've to create the data by using Bruno and your application~) but you can still access it!

step 10

View the data in your MongoDB Compass

  1. Download MongoDB Compass
  2. Add in the same URL that you've added into your .env file as DATABASE_URL
  3. Connect! ~