Full Stack React — Part 4(React, GraphQL, Express, MongoDB)

Episode 4: Install MongoDB (using Homebrew) (Evernote Clone)

Photo by Sergi Kabrera on Unsplash

In the previous episode, we built our evernote clone schema. If you would like to check that out, you can find it here → Part 3

In this episode, we will get our database running by install mongoDB using Homebrew. However, you do have the option to use mLab instead if you do not want to install it on your local machine. Since mLab is supposedly planning on concerting into MongoDB, I’m going to regular MongoDB instead.

Agenda:

  • Install homebrew(MAC)
  • Use homebrew to install MongoDB
  • Handle DB and Network access through MongoDB
  • Connect to your app!

Install Homebrew

  • Go to Homebrew website and copy the command given inside root directory of terminal. (wait until finished)

you should be all set with homebrew after this command :)

Install MongoDB using Homebrew

STEP 1: Inside root directory of terminal, run this command (which of course, installs mongodb)

brew install mongodb

STEP 2: After that finishes (successfully I hope) then run this command (if your on a mac, make sure to put the sudo before mkdir) I don’t know the exact details but from a video I watched this command creates a folder to store our database data. Feel free to read more about that in the MongoDB docs. If everything went well you shouldn’t see any errors

sudo mkdir -p /data/db

STEP 3: Next, run the whoami command (I know sounds weird lol) you should see your username, this lets us know that this can have access to the DB.

STEP 4: Next, (almost done lol) run this command sudo chown -R $(whoami)/data/db what this command does it actually sets our user to have access to the database.

STEP 5: Once everything went well with previous command, run this command mongod it should look like this →

Not quite sure what this means but from the looks of it, it just confirms all we did. At least I think, again I’m not sure but we needed to do that command.

STEP 6: Last step would be to finally start the mongodb server. Keep the current tab your on open, and open a new tab then run the command mongo when you run it, it should look like this →

You’ll know when you’re connected when the highlighted area displays the server mongdb is running on. :) keep this server running when working with mongoDB. If everything went smoothly you’re successfully connected to mongoDB server :D

Handle Database and Network access

STEP 1: Head over to Mongodb website to get set up, create an account(its free!) if you don’t have one already. When creating an account it should set you up with a free cluster to start with. When all is good it should look similar to this →

STEP 2: Go the the “Database Access” section and add a user, this is the username and password you’ll use to access your DB. Make sure that user has read and writing privileges, this is under “User Privileges”. After that, add user.

STEP 3: Go to the “Network Access” section and add your local machine IP address. Click the “add current IP address” . It tells the DB that your IP address is the only one that has access to modify this database. If you were to choose “allow from anywhere” then anyone can access your DB and modify it.

Connect to app

STEP 1: In your “Cluster” section click on connect. Go to “Connect your application” copy the connection string because this is what you’re going to use to connect MongoDB to your app.

STEP 2: Open up your project folder (evernote), in the server.js file paste/write this code →

const mongoose = require('mongoose')mongoose.connect('mongodb+srv://ajeasmith:<password>A@cluster0-qauba.mongodb.net/evernote?retryWrites=true&w=majority', (err) =>{  if(err) return console.log(err)  console.log('connected to mongodb')})

The “password” area is were you would replace with your user(user DB password) password you created earlier.

To make sure its working start your server on the app by running yarn start inside project folder, you should see “connected to mongodb”. At this point you should be all set with mongoDB and ready to talk to the DB :)

Thats all for this episode, I hope you guys enjoyed :) if you would like to watch the video of this episode it can be found here → Install MongoDB or if you want to check out the whole series (including the UI and mind mapping portions I did for this app) it can be found here → Evernote Series

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store