Why do we need MongoDB? The reason I want to add your products in Mongoose is to show you how to install it locally, and also how it functions in the MEAN stack. Mongoose is an integral part of any responsive web app as it is going to house 2 important parts of our business site:
- Allows you, and only you to log in (Authorization and Authentication- Part 4 of this series), and
- It will hold all the data for your services (price, name, description, product number, etc.)
I do want everyone to bookmark This GIT repository
by nax3t (Ian Shoonover)
. We are going to use his Repo install to make this as simple and seamless for everyone as possible since pulling from his repo guarantees a clean and healhty MongoDB install. Lets get into the code now.
OPTIONAL SAFEGUARD: I personally like to keep verisons of my site that I know are working and functional before I make large upgrades (for example, I am on version 6.5 of my personal site that you are reading this on right now if you looked at my code base). I would advise everyone to duplicate your v1 folder and rename the copy to v2. This way you have preserved your v1 environment in case you type something wrong or can not find where you made an error.
Install MongoDB: Ok, so now that we have our v2 folder lets make sure we are in the root directory of our environment. If you are in either v1 or v2 folders make sure you hit the command line with
cd .. (that space is required or you will get a command error) so that it drops you back to your root folder. Now we need to make a file to store the code that will open and install the respository in our environment. Put this in your command line
Double click on the newly created file and then paste this exact code into file and save it (CTRL + S in Windows or ⌘ + S on a Mac):
In your terminal copy and paste this line:
sudo mv mongodb-org-3.6.repo /etc/yum.repos.d. You should see the new file you made dissapear (this code is actually moveing the Mongo install code above the root directory you see so you can install Mongo once and use it in all your projects in this environment). Then put this code in your terminal
sudo yum install -y mongodb-org and watch MongoDB get installed from the GIT Repo (If you are prompted to close your
mongodb-org-3.6.repo file please close it at this time).
Direct The Data: To tell your local verson of MongoDB where to store the information we are going to provide you have to run a few lines of code one at a time in your command prompt.MAKE SURE YOU ARE IN YOUR ROOT DOMAIN AND NOT IN A FOLDER DIRECTORY BEFORE WRITING THE CODE. In your command line type these lines in order.
- The first line is
mkdir data to create the folder data where MongoDB will look for your collections.
- The second line is
echo 'mongod --dbpath=data --nojournal' > mongod to create the path for MongoDB to follow to get to the data folder.
- The third line is
chmod a+x mongod tells MongoDB that you have both read and writing privliges to the database.
Congratulations you just installed MongoDB into you project and we can test to make sure that it went perfectly. Next to the tab of the terminal window you are in click the green plus icon and open a new termal window. Enter this code into that new terminal
. If you see a string of code pop up where it looks for connections to the database then we are good to go and you have MongoDB running in your background. As we go forward I will remind you to enter this line before we test our site going forward since if Mongo is not up and running when we test our site we will get an error. Also, make sure you kit CTRL + C
to exit or you will get your database locked on you.
Connect MongoDB To Your App: We need to add one include file under your express require statements that is:
var mongoose = require("mongoose");. And we need to add one line of code to tell the app.js file what database to use for our app. Copy and paste
mongoose.connect("mongodb://localhost/mybusiness"); directly under
app.use(express.json()); in your app.js file and save it. Now we have the Mongoose requirement and the database set up that we are using its time to go to the next step and define the models that we are going to use.