In this video, Emmanuel Henri goes through some basics of MongoDB and talks about the DB, collections, and documents. Then he installs a tool you use to test your DB.
- [Instructor] This course is not a full-on course on MongoDB. And if you want to follow a course on the subject, feel free to take the learning MongoDB course in our library. We'll briefly go over the basics and concepts around using MongoDB so you have a good idea of what we're doing when we implement the code. A MongoDB database is one big object containing collections. Each collection could be like a contextual item. Like for example, contacts for the database we'll create. Then inside the collection, we have documents which are the contacts themselves.
So my contact information will be in a document, yours, and another, etc. And then inside each document, we have the data for each contact such as address, telephone, email, etc. Document looks like a JSON object with a key-value pair approach or arrays of data. So where those Mongos fade into this? Well, Mongos will allow us to set a schema for a collection and predefine what each key-value pair takes as a type. For example, the first name of a contact will take a string.
But we'll cover this in more detail soon. So as we continue on our backend API, we'll implement these concepts, and they'll start making a bit more sense if it doesn't right now. But before we move on, I'd like to install the tool we'll use to test our MongoDB and make sure the data we have in our database matches our calls we make to the endpoints. So first, go to your browser. And go to robomongo.org. Like so. Then download Robo 3T.
This is a free tool, so don't worry about that. Click on download. Then select whatever operating system you are on. So if you're on Windows, download the installer. If you're on Linux, download this one. For my case, I'm on the Mac, so I'll download this one. Once you have it, click to open it. And drag the application here. For my case, I already have it, so probably will ask me to replace it. So I'll just do that.
Perfect. Once you're done, make sure to eject the installer. And let's make sure first that we have an instance of MongoDB running. So we just tested it earlier. We actually did MongoD and we saw we had MongoDB installed, but it's not fully installed yet because there's still missing the data directory which we'll make in a second. So let's go ahead and do that. So go into VS Code or whatever editor you're using.
Then open up a second terminal. So if you're using an external terminal, go ahead and create a second tab. Like so. And the first thing we want to do is create a new directory. So the command to create a new directory is the following. And let's just quickly go to the documentation here. If you go back to the install on MongoDB and go where we were originally, you'll see that just below the install here that we've done, you just scroll just a little bit, and you have this command here.
Make dir. So you need to make this directory before you run MongolDB. Otherwise it's not going to work. So let's go ahead and do that. Copy that command. Or just simply type what I'm pasting here. So mkdir -p /data/db. And let's make sure we are good to do this. Sudo and then the password. So if you get this permission denied to create this directory, just do a sudo and enter the password and it will be done.
So now that we've got a directory created, if you're running on a Mac, you may still need to sup permissions for it. So Mongo needs to have read and write access to that directory. So we'll do that. So the first thing, you want to make sure you're in the right directory first. So in my case, I believe I'm already in there. And then you do a command like so. If you already have root access, you don't need to do sudo. Like this. Zero, seven, five, five.
/data/db. And then another one. Sudo chown $USER /data/db. Because I've done this before, I may get an error like this. But if you haven't done this before, then you won't get this error. So then we can go ahead and start MongoDB by doing Mongo d. And Mongo is now listening on port 27, zero, 17.
So let's go ahead and start Robo T. Like so. Yes, that's fine. Open it. I agree. That's fine. So let's go ahead and connect to our Mongo database. So what this tool will allow us to do is basically take a look at the database as we add new things to it. We can take a look at the items. We can actually change them ourselves inside of that tool.
This is like Postman for MongoDB, in other words. So let's go ahead and create a new one. And yes, it's a direct connection. Let's call this one Mongodb. And this is definitely localhost. 27, O, 17 as we saw. That's the port it actually is running on. Save. And then connect. And we're connected to the MongoDB database. Once we create our database inside of our application, or our server, we'll see it here as well.
Okay, so now we got some basics around MongoDB and a tool we'll use as we implement our database. Let's move on.
- Setting up a project and a server
- Setting up a database and schema
- Creating POST, GET, PUT, and DELETE endpoints
- Serving files