Transcript from the "Creating a Mongo Document" Lesson
>> Scott Moss: Now I'm going to show you how to do some inserts and some queries.
>> Scott Moss: So the first thing we want to do is let's connect to the database first cuz we can't do anything until we connect. Once we connect to the database then we'll start actually adding stuff to the student collection, and then we'll see if we can retrieve it, basic stuff.
[00:00:17] So what we'll do is we will run connect, the function that we created. And remember, connect is just calling Mongoose.connect, which returns a promise, so we should be able to just say .then and that'll give us a connection. We probably won't need this connection, but we'll just reference it here.
>> Scott Moss: Cool, so now that it connects to the database, and just to be safe we'll just catch it and log it if something happens.
>> Scott Moss: All right, so once we're in the scope of the database being connected and we're good to go, we can actually start writing operations against the database.
[00:01:02] So using the student model, what I want to do is I just want to create one student. So there's a lot of ways you can do that in Mongoose, but the preferred way is just to use the create method on the actual model. So I can make a new student here by saying student = and I'm going to await this.
[00:01:22] I'm just going to return a promise, I'm going to use asynch await actually, it's just better. So I'm going to await this, and I'm going to say await Student.create
>> Scott Moss: So by using Student.create, all I have to do is pass in an object that fulfills the schema that I created above.
[00:01:45] And the schema above only has a firstName whose type is String. There's no validations, there's nothing else. So if I pass in an object that fulfills that schema, then I should get back a new Student. Well if you look at our schema there's no validations, there's nothing. I could actually just pass in an empty object and this would actually create a student.
[00:02:01] It would have nothing on it other then ID. Cuz when you create documents IDs are generated, but other than that there's nothing else on it. And that's kind of where you can see the flexibility of Mongo, so. But I'm just gonna ahead and pass in the first name of Tim.
[00:02:18] Cool, and then we'll just log this.
>> Scott Moss: So now I connect to the database, I'm going to create my first student, and then I'm going to log it. So, now in order to run this we need to check for one thing. One, we need to make sure our server is actually running, our Mongo server.
[00:02:40] The MongoDB instance itself needs to be running. For me, cuzI install it with Brew, I actually use this pretty cool thing. If you have Brew installed, I use something called Launch Rocket. And Launch Rocket, what it does is it scans your file in Brew and it shows you all the services that you have, and then you could just start them with the GUI like this.
[00:03:00] So I don't have to use a terminal to go and start and stop Mongo, I just use this GUI and it does it for me. This only works if you install it with Brew. If you didn't install with Brew, it's still the same thing. You just have to go in the terminal and run the same command that we had to run before, which is just gonna be "mongod".
[00:03:17] And for me it's not gonna run because it's already started. I'm already running it so it's not gonna execute it. If you're not running it, this should just hang and you should be running mongod. So make sure your Mongo server's started by running mongod
>> Scott Moss: Once that's good, then we just execute this file with just basic node.
[00:03:39] So I don't know what you named the file on your machine, but for me I just called it test js, so I'm just going to execute this file, which is just for me is going to be node test.js. You can see like I said you'll get some deprecation warnings because of what's happening with Mongoose and Mongo right now, so you can kind of ignore them for now cuz they're not gonna kill your app but what actually happened here is there is my student, it was created, and I logged it out.
[00:04:07] So let's look at this object and kinda dissect it a little bit.