This course has been updated! We now recommend you take the API Design in Node.js, v4 course.

Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Executing CRUD Operations" 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:

Before moving on to authentication, Scott spends a few minutes testing the current codebase by executing a few CRUD operations from the command line. He adds a new user, creates a category, and creates a blog post. He also explains how populations work.

Get Unlimited Access Now

Transcript from the "Executing CRUD Operations" Lesson

>> [MUSIC]

[00:00:04] All right so if we did all this, we should, now that we have our models, we have our controllers, we have our routes plugged up, we should be able to start this and start doing some CRUD on it. So, let's check it out. So, in order to check this out, first thing we need to do is make sure we have Mongo running, so I'm gonna type mongod, make sure that's running.

[00:00:23] The server won't start without it. If you try to start the server without the database running, it'll crash. So make sure that's running. And then another, I'm just gonna run npm start to start ours. Looks like my path is wrong. Is it up one more? API, it's util, not utils, sorry.

[00:01:01] Cool, so there we go. So our servers on, then in another one, I'm gonna make a request to it. So, let's do a get request, let's do a post request to local host, of first 3,000/api/user. So, let's create a new user. And, as we know right now, user only has user name.

[00:01:25] So, let' say right now username=person. Looks like we have an error, hold on. Where. Well, no error. I don't see any logs. Let's see, let me start that down. Start it up.
>> There's typos in local host.
>> The what?
>> There's a typo in the local host.

>> There's a typo. Okay. Typo's get me every time man, every time. There it is. Localhost. Cool, there we go. Created a person. Totally worked. If I wanted to create a category, and category has a name. And it equals cats. It's categories, pluralized right. It'll create cats category.

[00:02:43] Now if I wanted to get it, it should be as simple as Get local host 3000/api/users. Give me all the users. There we go. I've got a lot of users because I added more on the data base. So, don't worry about all the extra users here. It gave me all the users.

[00:03:10] Cool. So, you might know it'll be kinda hard to do CRUD with the posts model and that's because all of the stuff that's, why? I don't know how this thing keeps getting here, but it's not being right. I need to fix that. So yeah, it be kind of hard to do this here.

[00:03:35] While I guess we could because non of these are required so I guess we could. Although this should be required author. So let's do that, let's make a post. So what will do is let's grab a user's ID We'll grab that. And then what we'll do is, we'll say, post to api/posts and author= that, title= learn node 3 and text= bah, blah, blah, blah, blah.

[00:04:22] Boom. We created a new blog post. See, it belongs to this author here. All right, and now if we did our calculations correct, when I do a get request for this blog post, what I would see back, I wouldn't see this author billed hear, I'd see the author object.

[00:04:39] So, let's check that out. So now, if I do a get request with this post ID, I expect there to be an object on the author field with the author's object on it. The user name and stuff, so lets check that out. So, now if I get logo host 3,000/api/post/thatid, there we go, there's the author property, it populated it, that's what population does.

[00:05:14] It took that ID that was, that it is stored in a database. In the database, the post looks like this, the author properties thats an ID of an author, but when we call it turns it into this. It gives us that authors object, alright so that is what population does, and if we had categories in there it would do the same thing with categories each on in an array.

[00:05:37] Is that starting to make sense now? I know I've talked about it a lot, but this is the first time I've actually seen it. So, now we're able to do CRUD on all our routes and yeah.
>> Stupid question, you may have covered it. The underscore, underscore v that's added to all of those, where's that coming from?

>> That's coming from Mongo. Okay, that's something Mongoose uses internally to determine-
>> I haven't looked into it. Maybe the document version. Yeah, it's not important, yeah, yeah, it's not important. Well, I guess if it is important if you ever wanted to make an underscore, underscore vproperty on your document, then you'd have a problem.

[00:06:17] So, If that's something you really wanted then you probably can't have it. Any questions on that stuff, so we all should be able to do it correct now. And if you were having problems creating author, for some reason this theme, I even put it here in the comments array of ideas from the user, that should not be in the comments I don't even know why I did that this should not be an array as an author.

[00:06:40] It should be just one author. Actually, I know why I did it cuz I was gonna have it so where many authors can write the blog posts at the same time. That's why I did it, but we're not doing it, so. I was thinking way ahead. Like, I was writing a solution for like a step 20 on step 10.