This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Creating a Document" Lesson
>> Scott Moss: So using models, so we talked about the schemas and the schemas creating the models, but how do we actually use them? Well, like I said, mongoose models work very nicely with CRUD, extremely nice. And the models are gonna be normalized. It doesn't matter what schema it is, it's still gonna be the same model.
[00:00:17] They're still gonna have the same methods. It doesn't matter what the schema looks like, they're gonna have the exact same methods on. They're gonna work exactly the same. But basically, I'm just gonna break down some methods on the mongoose models and how they tie into CRUD. So the C in CRUD, which is create, some of the methods you would use would be model.create, or just use new model.
[00:00:36] So I'm gonna walk through those right quick. So if I go into item.controller, which is where you're gonna be working and I make a create, actually I'm not gonna do it here. I'll do it in, yeah, I'll just do it here. It's just an example. So if I wanted to make a create function here, so I would say, import from item model.
[00:01:10] So if we go look here, so the create inside of CRUD, we can just do model.create, which is the one that I use the most. You can also do new model, which doesn't really create a model but it creates one that's ready to be saved. So, if I wanted to make a new model, I could just say item.create, like this.
[00:01:29] And then I was passing the fields that an item needs. In this case, we know an item needs, it needs a name, that's required. It needs a status, that's required but it has a default so we can skip it. Doesn't need notes, doesn't need due date, but it does need created by and it needs list.
[00:01:45] So we have to pass a created by, we have to pass a list, and we have to pass a name. So I would pass all three of those here. And then, boom, I have an item. This is asynchronous, so you'd either have to do a promise or async or wait on it.
[00:01:58] Yeah, it'll create it for you in database. So that's the C in CRUD for Mongo. Item.create. So this takes this one item that you wanna create or any model, or it can take an array of them. So it can create many of them,
>> Scott Moss: Like that. So again, this doesn't really matter.
[00:02:19] If this was list, this would work the same. If this was user, it would work the same. Now, if your validations were different, then those would throw errors, right? If the user had a different validation than the list item, then obviously, that wouldn't work. But as far as it executing, it'll execute exactly the same.
>> Scott Moss: Because that's what the mongoose models do, they normalize our schemas so that we can use them generally, like that. So that's for the create. And then next we have read in CRUD. So read, some of the methods you can use there are gonna be find, findOne, findById.
[00:02:55] Those are gonna be some ones that you're gonna be using in Mongo. And they're pretty simple. So if I wanted to find one, actually I'm just gonna go ahead and run some of these in a file, versus just virtualize them. So I'll just make a file down here.
>> Scott Moss: Well, I guess I can just do it in here. It doesn't matter. There we go, so.
>> Scott Moss: Correct, there we go. So I'll make a create.
>> Scott Moss: All right, let's call this.
>> Scott Moss: Cool. So for the create, I'll just say this is an item that we created, and this'll just be await item.create.
>> Scott Moss: And we know we need a name, so I'll just give it a name, call it Clean up, if I could type. And we know we needed a createdBy, I don't have a user right now so I'm just gonna just put a fake ID in there for mongoose.
>> Scott Moss: Which is gonna be mongoose.Types.ObjectId, which creates a fake object ID. And then list is the same thing. I don't have one, so I'm just gonna make a fake one as well.
>> Scott Moss: There we go. And now I will just log this.
>> Scott Moss: I'll just run this function.
>> Scott Moss: Cool, so now I wanna execute this.
>> Scott Moss: Cool, so before can run this, we need to connect to the database, otherwise it won't run. And in the utils folde, there is a DB file that has a connect function that will connect for us. So we can just use that.
[00:05:08] So that's exactly what I'll do. I'll go over to our file that I was writing here, and I'll import that functionality from util/db, the connect function. And I'll just go ahead and await that connection, because it is asynchronous. And then from there, I'll just give it a mongo url, you can give it any one you want.
[00:05:34] Local host.
>> Scott Moss: 27017. And then any database string. So once we connect, await the connection. If it's successful, then we should see this thing create. So let's build this,
>> Scott Moss: And then run it. So looks like it ran. And as you can see right here, that create method did create an item in the database, with our defaults and the IDs that we passed it.
[00:06:07] So that's the seed for create.