Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Mongo Introduction" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

Mongo is a NoSQL document store. Data does not have to be modeled in advance with Mongo which makes it very easy to setup and use. Scott demonstrates how to create a new collection and insert records using the Mongo command line interface.

Get Unlimited Access Now

Transcript from the "Mongo Introduction" Lesson

>> [MUSIC]

>> Speaker 1: Now we're gonna start getting into Mongo, and actually saving data and mess around with that thing. And figuring out how it works and how to connect it to our API and get things going. Before I get started were there any questions about what we already covered before lunch today?

[00:00:18] That you might have missed that you might wanna ask now?
>> Speaker 1: Nope. Well, all right, let's get into Mongo. So does everybody have, who doesn't have Mongo installed or hasn't tried it? Tried to execute it, try to run it? Cuz if you didn't, you might be surprised that it probably doesn't work.

[00:00:44] Okay, good so far, hopefully everybody online is in tuning in just downloaded it and installed it. Great so, Mongo is a NoSql document store, who here wants to give me the definitions of NoSql?
>> Speaker 2: Not meaning sequel.
>> [LAUGH]
>> Speaker 1: Sorry, it's not, it's not sequel meaning it's non-relational.

[00:01:09] It's not a relational database, we don't get those guarantees. Those fixed with columns and stuff like that. It's just a document store, meaning it's just a basket where we can throw stuff in. That's pretty much what a document store means. We don't have to model our data. And we can just throw JSON in it and ask for it later.

[00:01:29] That's Mongo, just give me some JSON, I'll save it and then I'll give it to you later when you ask for it. That's pretty much what Mongo is.
>> Speaker 1: We need a way to persist our API data, so we'll be using Mongo for that, obviously.
>> Speaker 1: So it's actually pretty simple to set up and use.

[00:01:51] Hopefully everybody, like I said, already downloaded it. Note that it's not the best Database or options for every situation just like anything else out there, but for the sake of this project it's perfect it's gonna be great for what we're using it for. So, if you have Mongo installed, then you can type in this command mongod or mongod.

[00:02:18] What people say in a terminal window to start the database. So like if we go here, open up a new shell, and type in mongod hit enter, you should see this something like this. Pretty much like on port 27017. So if you're in Windows you might have to use the execution path a little differently.

[00:02:40] But when you execute it and you don't see this and it's not hanging, you get an error and it just stops then you didn't do something and something's not right. So if you don't have that then you didn't try to run Mongo and it's not working. So this just started the Mongo database on that port on that server.

>> Speaker 1: So everybody should have that. The next thing is we could also interact with Mongo that we just started through what's called a Mongo shell that it ship with, and it's actually pretty awesome. So to do that, we can open up another window with Mongo still running, it's still running over here.

[00:03:19] We can open another window and we can just type in mongo without the d and if I spelled it right it'll say something like this. It'll tell you what shell version you're on and it will automatically connect it like some database usually test as a default. This is kind of like our rebel, we can do JavaScript like things in here.

[00:03:39] Because Mongo really runs off JavaScript, it's built with JavaScript, we can do JavaScript things. So this is how we can interact with our Mongo so for instance if I wanted to see all the databases that I have. I can type in show db's and it would show all the databases and all their sizes on my machine.

[00:03:56] I had like 30. I had to go delete them all. Cuz I had some other databases on here. So these are the three databases on my machine. There's a whole bunch of other stuff you can type in. You can just type in mongo--help. It'll show you all the types of stuff.

[00:04:11] So yeah, that's the Mongo shell. We can do JavaScript like things in here to interact with that database, show dbs is just one thing, show all the databases. Cool, so once you do that, it will connect to the test database by default, which, you saw when I did it.

[00:04:29] So now we can use JavaScript to run queries against whatever database we connect to. So what we're gonna do is exercise. And if you have Mongo running, you can all just do this with me together on your machines. We're gonna create our first collection. So a collection is like a table.

[00:04:43] Just think of it as a place where Mongo is going to store alike things. So it's kind of like a table. But I have a collection of people, I have a collection of dogs, a collection of to do's, so I varied like data pieces. So it's like a group of data is modeled sameish actually that's not a typo I actually meant to spell it that way " sameish".

[00:05:06] So, that should be a word. So, lets just do this together so the first thing we're gonna do is. We're going to make a data base and we're going to switch to it at the same time. So to do that we can just say they used. And then whatever name for the database you want.

[00:05:19] In this case, I'll call it puppies. So, by saying use puppies, I am going to make a database called puppies and then now I have switched to it inside the REPL. By the way, when I say REPL, I mean read, evaluate, print, loop, REPL. So, I made a database called puppies, you can name it whatever you want.

[00:05:44] Now if I say show db's, I guess it won't show until I close it and open it back up I guess. Let's see. Still not showing up. But, I guess I had to put something in it, but it's there. So we're gonna say use Puppies, sweet. The next thing I'm going to do, is I'm going to create a collection and the example says to do's but, you do whatever you want.

[00:06:10] This example is going to be whatever arbitrary string that you want, so to do that you use db. CreateCollection CamelCase.
>> Speaker 1: Collection, and then just pass in a string. So I'm gonna make a collection called
>> Speaker 1: toys, and then you'll get this thing that says, ok. That means it did it.

>> Speaker 1: All right, and then to verify that you can say, show collections and then you should see the collection that you just made. Right under system.indexes. Indices, indexes whatever. Everybody got that? You've made a collection, it said okay, then you said show collections and then it shows you the collection that you just made.

>> Speaker 1: Cool, so then we're going to insert Document into that collection. So document is an instance of a collection. It's an actual thing, the data that we'll be doing. It's the resources that we'll be using in our APIs, the documents. So we're gonna insert our document, so let's say db.

[00:07:32] the name of our database. In my case it's called puppies. .insert, that's a function, and you just pass in an object here, whatever you want to. So since this is, I'm sorry, not db.puppies, I'm sorry, the name of the collection in this case, toys, And I'll give it a name, it's a yoyo, dogs like yoyos and a color, it's red.

[00:08:04] And if you hit enter, you won't see anything. It won't say anything. It will just go to the next line. And if it's an error, then you probably messed up. If it's not an error, you're probably good. Yeah, we'll figure out right on this next command. So the next command is, we're just going to query that collection to see what's in there.

[00:08:30] So to do that, the easiest way is to db., the name of your collection. In my case, again, this is toys. And I can say .find with no arguments. And I can execute that, and then it's just put back the thing, the, the, all the instant, all the documents in that collection which should just be one.

[00:08:55] Everybody see that? Everybody be able to query their document that they just saved?
>> Speaker 1: Also, if you're on Mac, there's this program called Robomongo, which is just like a GUI for Mongo so you can connect to it, go in there. Here's my puppies database. I can go to collections.

[00:09:22] I can go to toys. [COUGH] And then I can see the toy that I just made right there. So this is GUI you can use, it's called Robomongo, Robomongo. I'm not sure if they have it for Windows or not but it's definitely on Mac and it's free, okay.

>> Speaker 1: So that's an intro into Mongo. I don't know how much experience people have with databases, but that's pretty easy to save something in a database in my opinion. Don't have to worry about migrations and stuff like that, you just throw some data in it. And ask for it later.

>> Speaker 1: That's why I chose this database for this exercise, it's pretty easy to get started with.
>> Speaker 1: Okay. So here's just like a walk-through of what we were doing. So by saying the keyword use followed by the name of the database we want Like I said, Mongo will switch to it if it exists.

[00:10:32] If it doesn't exist, it'll create it and switch to it. So run those commands one by one until we get the stuff.