Are you new to MongoDB? Perhaps, like me, you came from a SQL based database? I am writing this article to help get you started with MongoDB as it is a little bit different than SQL and the set up can be confusing at first. Let’s dive in!
Stack
The stack I will be using for this article is MERN although React won’t be necessary. Lets set up a new project first. First make a new directory to house your project. I called mine mongodb-setup
so:
mkdir mongodb-setup
Setting Up Your Server
Once your directory cd
into it and
npm init
to integrate Node and any npm libraries. Now lets install the mongoose library
npm install mongoose
Mongoose is the library we use to control, manipulate, and pass data to and from our MongoDB database. You should see a node_modules directory create in your project.
Next, we need to create a file that controls all server related tasks.
touch server.js
Since we are going to use Experess to host our server lets install that as well.
npm install express
Now lets set up our server.
At the top of server.js
add
const express = require(’express’)
underneath that add
const app = express()
Finally, the last thing we need to do is connect our server. Add
app.listen(8080, () => { console.log(‘A Node JS API is listening on port 8080’)})
Now run npm start
if your server was set up properly you should see the message A Node JS API is listening on port 8080
appear in your console which means express was set up correctly. Congrats!
Using MongoDB Atlas
In this section we are going to utilize MongoDB atlas to create a cluster that will house all of our data. Head over to https://www.mongodb.com/cloud/atlas. Make an account
Once you sign in you will be brought to the cluster selection page. If you are just testing out the database then you probably want to select the free version.
From this page you can choose a provider. In this case I am picking AWS, then select a recommended location below. Then click CREATE CLUSTER`
It will take a few minutes to create the cluster. In the meantime, lets setup a .env file in our app. In your app type
npm install dotenv
in the terminal. Next,
touch .env
to create the file. Inside of .env lets get the DBURI set up. Add
MONGO_URI=
we will be adding something here shortly, but for now let’s leave it at that.
Now, let’s configure the cluster so that we have access to the database. Head over to Database Access under the Security section on the left.
We need to add our self as a user in order to grant access to the database. Now, create a username and super-secret password and click Add User.
Next, we need to whitelist your database so that it will accept and grant access to requests. Head to Network Access directly under Database Access
Click ADD IP ADDRESS then, ALLOW ACCESS FROM ANYWHERE. Finally, click Confirm.
Now, let’s connect our database to our app. Head back to the Cluster tab and select the connect button. This will bring up a modal on which you will select Connect Your Application.
Make sure you copy the connection string provided by clicking the clipboard.
Linking App to Database
Now, we can connect the database to our app. Head back over to your app and open the .env file. Lets add the connection string to MONGO_URI. So in my case this is what I’ve got:
We need to change to the password we created when we added ourselves as a user. Notice how my code changes:
Next, we need to add a few more packages. In server.js add the following:
const mongoose = require(‘mongoose’)
const dotenv = require(‘dotenv’)
dotenv.config`
Remember, mongoose is a library for MongoDB. The dotenv configuration is so that we can use our MONGO_URI constant from the .env file.
Lastly, We need to add two more blocks of code to finish this connection. Below that exemplifies those final blocks. You can just copy it.
Our finished product should look like the following:
Once that is completed run npm start
you should see the messages “A node JS API is listening” and “DB Connected” in the terminal. If you’ve got that then congratulations you are done.
You are now able to make requests to your database! If you find yourself getting stuck you can visit my repo here: https://github.com/Aaidenplays/mongodb-setup
I hope you found this article helpful! Thank you for reading.
Top comments (0)