Mongo is a NoSQL document store. Data does not have to be modeled in advance with Mongo, which makes it very easy to set up and use. Scott demonstrates how to create a new collection and insert records using the Mongo command line interface.
(asian music) - [Scott] Now we're going to start getting into Mongo. And actually saving data, and messing 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 we've already covered before lunch today, that you might have missed, that you might want to ask now? Nope? Well, alright, let's get into Mongo. So does everybody, who doesn't have Mongo installed? Or hasn't tried it, tried to execute it and tried to run it? Because if you didn't, you might be surprised that it probably doesn't work.
Okay, good so far. Hopefully everybody online tuning in has downloaded and installed it. Great. So Mongo is a NoSql document store. Who here wants to give me the definition of NoSql? - [Student] Not made in SQL. - [Scott] (laughs) That's right, it's not SQL, meaning it's non-relational, right? It's not a relational database, because we don't get those guarantees, those fixed-width columns and stuff like that.
It's just a document store, meaning like 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 could just throw json in it and ask for it later. That's Mongo. It's 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. We need a way to persist out API data, so we'll be using Mongo for that, obviously.
So it's actually pretty simple to setup and use. Hopefully everybody, like I said, already downloaded it. Note that it's not the best database, or option for every situation, just like anything else out there. But for the sake of this project, it's perfect. It's going to be great for what we're using it for. So, if you have Mongo installed, then you can type in this command mongo d, or mongod, that's what people say, in the 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 it like on port 27017. So if you're on Windows, you might have to use the execution path a little differently, but when you execute and you don't see this and it's not hanging, you get an error and it just stops, 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. So everybody should have that. The next thing is we can also interact with Mongo that we just started through what's called the Mongo shell that it ships with, and is actually pretty awesome. So to do that, we can open up another window with Mongo still running, it's still running over here. We can open another window, and we can just type mongo without the d, and if I spell it right, it'll say something like this.
So what we're going to do is an exercise, and if you have Mongo running and everything you can all just do this with me together on your machines as we're going to create our first collection. So a collection is like a table. Just think of it like a place where Mongo is going to store alike things. So it's kind of like a table. It's like I have a collection of people, a collection of dogs, a collection of to-do's. It's like very alike data pieces. It's like a group of data that is modeled same-ish. Actually that's not a typo, I actually meant to spell it that way, sameish, so that should be a word.
So let's just do this together. So the first thing we're going to do is we're going to make a database, and we're going to switch to it at the same time. So to do that we could just say use and then whatever name of the database that you want. In this case, I'll call it puppies. So by saying use puppies, I'm 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. Now if I say show dbs, I guess it won't show it until I close it and open it back up I guess, let's see. Still not showing it but, I guess I have to put something in it, but it's there. So we're going to say use puppies. Sweet. The next thing that we're going to do is I'm going to create a collection. In the example it says todos, but if can be whatever you want. This example just has todos, it can be whatever arbitrary string that you want.
So to do that, you use db.createCollection, camelcase. And then just pass in a string. So I'm going to make a collection called toys. And then you'll get this thing that says ok. That means it did it.
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, indicies, indexes, whatever. Everybody got that? You made a collection, it said ok, and then you said show collections, and then it showed you the collection that you just made. Cool? So then we're going to insert a document into that collection.
So a document is like an instance of a collection. It's like the actual thing, the data that we'll be doing. It's the resources that we'll be using in our API's, the documents. So we're going to insert our document. So we have to say db. 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 on it. So since this is, I'm sorry not db.puppies, the name of the collection, in this case toys, db.toys.insert, and I'll give it a name, it's a yo-yo, dogs liked yo-yos, and a color, it's red.
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 you got some output, and if it's error, then you probably messed up, if it's not an error, you're probably good. And 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. So to do that the easiest way is db. the name of your collection, and then in my case, again, this is toys, and I'm going to say .find with no arguments.
I could execute that, and then it should spit back the thing, all the documents in that collection, which should just be one. Everybody see that? Everybody able to query their document that they just saved? 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, and I can go to toys, and then I can see the toy that I just made right there. So this is a GUI that 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.
So that's like an intro to Mongo. I don't know how much experience people have with databases, but that's pretty easy to save something into a database in my opinion. We don't have to worry about, you know, migrations and stuff like that. We just throw some data in it and ask for it later. That's why it shows this database for this exercise. It's pretty easy to get started with. Okay, so here's just a walkthrough 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, if it doesn't exist it'll create it and then switch to it. So run those commands one-by-one until we get the stuff.
This course was created by Frontend Masters. It was originally released on 12/30/2015. We're pleased to host this training in our library.
- Executing Node.js
- Using Express
- What is middleware?
- Testing in Node.js
- Using Mongo with Node.js
- Data modeling
- Querying data with Mongoose
- Identifying sensitive routes
- Configuring the deployment