Check out a free preview of the full Introduction to MongoDB course:
The "Course Setup" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Scott gives a quick tour of the Github repository, and what the course will contain. Instructions are also given for how to download MongoDB, how to start the Mongo database server locally, and an introduction is given for the Compass GUI.

Get Unlimited Access Now

Transcript from the "Course Setup" Lesson

[00:00:02]
>> Scott Moss: All right, so thanks for joining and coming into the MongoDB course. Super excited to teach your about your first time using Mongo and how awesome it is. So to get things started, the first thing you need to do is hop into this repo, which is gonna be at FrontendMasters/intro-mongo-db.

[00:00:20] And just a quick tour of what's in this repo and how we're gonna go about these exercises. Basically, this readme's gonna have everything for you, but let's just walk through it right quick. This course is basically gonna walk you through the intro of Mongo, we're gonna be using in a Node environment.

[00:00:35] And we're gonna cover things like queries, creating stuff, different indexes, all different types of filters, so we'll be working on stuff like that. And it's gonna be broken down into different exercises, so the repo is gonna have a folder for every exercise. So if you click into the exercises folder, you'll see different folders for each exercise.

[00:00:54] And we'll be going through these in a sequential order. And I'll tell you which exercise that we'll be working on at the given time. But most of the time, we're just going to be following the instructions in the readme. It's gonna have some command that you need to run, some tests that you need to pass, and some checks that you have to check off.

[00:01:09] And that's gonna be, we're gonna be talking about the stuff in that exercise before you get to do it, so you'll have the proper context. So once you have this down, whether you fork it or clone it, just get it on your machine. Make you run an mpm install to get all the modules installed.

[00:01:23] Once you're good there, the next thing we need to do is actually install Mongo. So there's a few ways to install Mongo, and it's gonna depend on what machine you have. Whether it's Linux, Mac, Windows, whatever flavor you're using. You probably are gonna be able to do it from the source.

[00:01:39] And the creators of Mongo actually have a really nice setup and tutorials and documentation on how to install it in many different ways. So if we head over to their website, at mongodb.com/download-center, the next thing you wanna do is, you wanna click on Community Server. That's what they call it, Community Server, that's actually MongoDB, the one you wanna use.

[00:01:58] So go ahead and go there, click on the OS of your choice, for me, I'm OS X. And from here, looks like I have two options. I can install with Homebrew, which is what I already did, I use Homebrew to install most of my stuff, but you can also download by the .tgz.

[00:02:13] I recommend installing by Homebrew if you're using Mac, it's just way simpler. If you're using Windows, I recommend downloading the MSI file and going down that route. And they have installation instructions on how to do that.
>> Scott Moss: So once you have Mongo installed on your machine and you're ready to go, the next thing you wanna do is verify that installation.

[00:02:36] So what we can do is, we can head over to our command prompt if you're on Windows, or terminal if you're on Mac. And basically, what you wanna do is, you want to run the mongod command. And the mongod command is gonna start your Mongo Database server locally on your computer.

[00:02:51] I'm gonna run it, and it's gonna crash for me because I'm already running Mongo in the background, so it's a conflict of port. But once you run this for the first time, you'll see that it just hangs and it sits there. So once you have that going, you wanna open up another window, and you wanna run the mongo command, without the d.

[00:03:08] And that's gonna open an interactive shell like this. So this shell is basically an environment that uses a hybrid of Node.js and some type of JavaScript and some other pseudo language to interact with Mongo and all the different databases on your machine. So we're just gonna walk through some quick things to just get a feel around this shell and experiment with Mongo.

[00:03:30] So the first thing you wanna do is, you wanna type in this command, which is gonna be show dbs. So show dbs, which is short for databases. Once you run that, it's just gonna list all the databases that you may or may not have on your machine. If this is your first time installing Mongo, you might only see these three.

[00:03:45] These databases were created by Mongo, and they store information about internal Mongo stuff, so they weren't ones that you created. So what you wanna do now is, you wanna switch to a database. We don't wanna use any one of these databases, so we'll just go ahead and create one.

[00:03:59] And the beauty of Mongo is that we can create a database on the fly when we switch to it. So what I can do is, I can use the use keyword, and then put in a name of a database. So I'm just gonna make up one, and I'm gonna call it todos.

[00:04:13] And this is not only going to create a database called todos, but switch to it. So you can see right there, it switched to a database called todos, so it created it, and now I'm actually using it. And to verify that, I can do something called show collections.

[00:04:28] And actually, you're not gonna see anything because we don't have any collections. But this tells me, because this command didn't error out, that I'm actually using a database. You can think of collections like tables in C code, very similar to that, and we'll get into collections a little later.

[00:04:43] For right now, just to keep this example going, we're gonna see some of the different commands that we can run in the show. So to do that, we can just do db.help(), and we can invoke it like a function. Cuz remember, I said it's kinda like pseudo JavaScript, kinda Node things happening here.

[00:04:58] And you can see all the different methods you can run on the database. And there's some pretty helpful methods in here. But again, this shell is really just to test and play around with what's actually happening in a Mongo environment. Some of this stuff right here, you probably wouldn't really use in production, you'd use something like a GUI, which I'll show you in a minute.

[00:05:15] So for things like creating a collection, you can see, there is a method here to create a collection, and we can try that right now. So db.createCollection, and it takes a name, and I'll just call it items. And you can see it said ok true, so now if I type in show collections, I see that I have a new collection called items.

[00:05:36] Again, this is just to play around and explore Mongo, I recommend not using it, you can make a lot of mistakes in here. I'm prone to make a lot of mistakes in here, especially if you're connected to a deployed server somewhere. So highly recommend not doing it, you'll want to use a GUI to actually interact with the Mongo database installation on your computer.

[00:05:55] It's much friendlier, less error-prone, it actually has a lot more tools for advanced features. So here, what you wanna do is, you just wanna click on the Compass tab, and MongoDB Compass is the GUI that MongoDB, the creators of Mongo, actually created for us. So it's free, they have a paid version, but you could just use the Community Edition.

[00:06:15] That's what they're calling the free stuff, the Community Edition. So pick the latest version, pick your platform, and download it. Once you have that downloaded, you can go ahead and open it. I already have it downloaded, so I'm gonna go ahead and open up MongoDB Compass Community here.

[00:06:30] And most likely, what you're gonna do, I'm already logged in, so I'm actually gonna close it and open up again so we can get a fresh version. So you can see it's gonna load like this. And once it does its thing and it loads up, eventually, it's gonna ask you to connect to a database.

[00:06:45] So you're gonna be prompted with this screen. If you have databases already, you'll see them on the left, favorites that you might have. So for me, we're just gonna connect to a new one, and by default, I wanna connect to the one that's on the local host on my machine.

[00:06:58] By default, the port to every Mongo database is 27017, so I don't have to change any of that. So I can actually choose to go ahead and just click Connect with all these defaults, and it's gonna connect to local host. And you can see those four databases that we had in the shell are listed here.

[00:07:12] So the admin, the config, and local, and then the one that I created is todos. So I click on todos, you can see there's the collection that I created. And once I click on that collection, there's all different types of things that I can do in here. I can insert documents, I can do this weird thing called aggregations, this explain plan, indexes.

[00:07:29] So it's very useful, has a lot of meta information. And it's not just for local databases, you can use it for deployed databases as well. So highly recommend using MongoDB Compass.