Check out a free preview of the full Complete Intro to Databases course

The "MongoDB" Lesson is part of the full, Complete Intro to Databases course featured in this preview video. Here's what you'd learn in this lesson:

Brian introduces MongoDB, a NoSQL database, demonstrates how to create a MongoDB database using Docker, and defines collections as a group of documents or a group of objects. It is best practice to have one collection to represent objects with the same characteristic. A collection would be a row within a database. Row, record, and document are terms that can be used interchangeably.

Preview
Close

Transcript from the "MongoDB" Lesson

[00:00:00]
>> There are lots of other NoSQL databases. And they come out fairly frequently. Today we're gonna be focusing on one called MongoDB, which if you're a JavaScript developer, probably sounds fairly familiar to you. But there are plenty of others to look at like Cassandra, Couchbase, Rethink, there's a myriad of other ones.

[00:00:18]
This one's just very popular with specifically JavaScript developers, but in general with a lot of companies. So let's get into talking about MongoDB. I think it's pretty safe to say that MongoDB is one of the most popular databases today in the world. It's been a pretty reliable custodian of data for over a decade now.

[00:00:38]
And it works really well with very small workloads like when I'm just doing little one off projects myself, I very frequently choose MongoDB just because it's really easy to get up and running with. The one thing I'm gonna say is if you go look through MongoDB history, they've had a couple of rough patches of like there's a time where they were dropping data for a while, they had some rough patches, I'll just put it that way.

[00:00:59]
I'm just gonna say that they're past that they haven't had a real problem in a long time. So I feel very confident in recommending to all of you. MongoDB is a very good use or as good database for whatever workload you're gonna throw at it. So let's go ahead and just dive into it.

[00:01:19]
So hopefully you have Docker Desktop up and running now. You can tell if it's up and running, here in my Mac, you can see I had this little whale up here. You can see here it says Docker Desktop is running. Hopefully that's something you see right now. If you do, then great.

[00:01:39]
If not, then you'll have to get it up and running. On Windows, I think it's in your system tray. Yeah, it'll be in your system tray as well. And then we're gonna be running some commands that I have here just listed out for you. This should work in PowerShell.

[00:02:01]
I didn't actually try it in PowerShell, but this should work in PowerShell. If not, you can use WSL and get into bash and the shell. This will definitely work through bash in WSL, which stands for Windows subsystem for Linux. If you just search for WSL Windows, it'll show you how to get that up and running.

[00:02:20]
So I'm gonna copy this Docker run. And I'm gonna open my terminal here. And I'm just gonna paste that in there. Docker run--name test-mongo, you can call it whatever you want, I just call it test mongo because I'm not creative. -dit, that's for detach interactive and tty that's what the dit stands for.

[00:02:44]
-p this this is gonna be what port you're gonna be opening from your Docker container running on your computer to your local computer. So we're opening the port 27017, outside. So that's gonna be the ones gonna be available my host computer, and it's gonna be opening 27017 which is the default port for Mongo, interior on the inside, all right?

[00:03:04]
So you can kinda map those ports if you want them to be different. I don't, so that's why I'm saying that. The --rm just means if, when I'm done with it just remove cleanup automatically afterwards. And then the name of the container that will be pulling off of Docker Hub is Mongo.

[00:03:23]
And then the :4.4.1 we're pulling the version 4.4.1. So if you run this, you should just get something like this. This is the hash of what's running in side of docker. So if I type Docker PS right now, you can see I have one container running which is this one you can see these numbers match up.

[00:03:47]
You can see it's forwarding this port here. And you can see the name of this is test Mongo. So now we have one container running on our computer. It's running MongoDB. It's available for us to access. I don't actually have the Mongo client installed on my computer, but the container does.

[00:04:02]
So we can kind of cheat and use the containers own Mongo to query against itself. So if you go back here to my notes at this Docker exact command right here. And I'm gonna be connecting to my test Mongo container, and I'm gonna be running the Mongo command.

[00:04:21]
And now you can see here I am inside of Mongo. So I'm now actually able to query against Mongo. Now we're inside of MongoDB. And now we're able to start writing quit all database clients, or all databases have these kind of clients, we can get into the command line and start doing queries.

[00:04:38]
So let's look at one of these. If I do show DBs like that, you can see here I have three different databases. One called admin, one called configure and called local. This is what Mongo creates for you by default without you having to do anything. So Mongo has two kind of key concepts here.

[00:04:58]
One is called a database and one is called a collection. And if you're familiar with something like my sequel or Postgres or something like that, you'll be familiar to databases and tables. So a table and a collection are roughly analogous across MongoDB and like a relational database. So the first thing is we wanna create our own database.

[00:05:21]
A database is going to be a group of collections. So let's say you had an app that was running like a pet adoption, which is what we're gonna be doing, I always talk about pet adoption, because I like dogs. That's that's really the crux of that issue right there.

[00:05:37]
We're gonna be using a database called adoption because we're gonna have multiple different collections inside of this database. So it's a group of collections. You'll find frequently your application will only ever use one database, that's okay. If you have a bigger application has a bunch of, this is users, and this is movies, and this is TV shows and you need to separate those out, you could have multiple databases which would then have multiple collections.

[00:06:00]
It's all about organizing your data in a useful way. But the idea is that you wanna have like data together. So I'm gonna say use adoption, like that. That's gonna be the name of my database because it's gonna be a pet adoption database. And you can see here now I've switched to DB adoption.

[00:06:19]
That didn't exist before, but again, Mongo is dynamic in nature, so it just rolls with it, rise like you want something called adoption? Here you go. I don't actually have something like that, but you can have it now. So let's talk about collections. Collections are just basically like a grab bag of documents, right?

[00:06:36]
A document is gonna be like a record or if you're thinking about a spreadsheet, it's gonna be like a row, right? So one individual entry into your database is going to be a document and the table of those rows is going to be your collection. So yeah, it's just a grab bag of objects, you can think of it as basically JavaScript's objects.

[00:07:01]
In fact, literally, the query language for MongoDB is just JavaScript. So if you know JavaScript, you already know how to do querying in MongoDB. So for example, I can just type math.now like that. That's date.now, not math.now, you can see, this is actually just JavaScript. So you can actually just run JavaScript stuff in here.

[00:07:28]
So math.max, that would work, right? Now, we'll actually just be copying and pasting some JavaScript in here to generate some records for us. Again, I'm gonna be using the terms, the correct term to use here is documents. That's what each of these are. But I will use the term documents and records kind of interchangeably, just kinda fixed in my brain.

[00:07:49]
Those terms that mean the same thing. It's one entry in your collection. So let's make a database of pets, and let's insert a awesome record into that. So I'm gonna say DB, that's gonna represent adoption, right? So that's kind of like the object that represents adoption right now.

[00:08:11]
And I'm gonna say .pets, that's gonna be the name of my collection that I'm about to create. And I'm going to put an insert one, like that. And this is a function that you just give it a JavaScript object and it's gonna turn that into a record or a document in our database here.

[00:08:30]
So it's gonna have a name and this name is gonna be Luna because it's obviously gonna be my dog. It's gonna be a type of dog. Its breed is going to be Havanese and she is eight years old. So that should look pretty familiar to if you've ever written JavaScript before.

[00:08:55]
It's because it's literally just JavaScript. Okay? So if I hit Enter that now, you can see, it acknowledged, which means that the query was successful. And it inserted a new ID and that object has an idea of blocks. So every documents that you insert into MongoDB, it just automatically gives it an ID.

[00:09:18]
It has its own kind of internal ID system. Whereas with other databases you have to create your own IDs, MongoDB automatically has one for you. Let's take a look at that record then. So first thing we'd say db.pets.count. And you can see now this has one record in it.

[00:09:40]
And if I say show DBs now, you'll notice that I do have an adoption database because it created that for me when I inserted that first document. So now it's like okay, now this is real and it goes and actually creates all that stuff for you. Another thing that's gonna be helpful for you is you can also say db.pets.help.

[00:10:00]
And this is actually going to show you all of the commands you can run. So you can do find, find one, get db, get indexes, all that kind of stuff. I frequently forget all this stuff and it's nice to just be able to kind of see that at a glance.

[00:10:14]
I think that also works for db, we just check it does. So you can see all the things that you can do with the database as well in addition to the collection. We can do things like DB like stats well I think that works, right? So this is, it has one collection, one object inside of it, the average side of it.

[00:10:32]
There's a bunch of stuff you can do kind of administrative stuff. Yeah.
>> I keep hearing the same record as does the same as document?
>> It is yeah. Those terms are not necessarily interchangeable. But l also can't get away from saying because I just kind of the word that comes to mind.

[00:10:56]
But it's one entry in a database can be considered a record. It can be considered a document, a row. You'll hear it called all those different things. The correct term here is document.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now