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

Scott walks through the solution to exercise 7. He also briefly discusses collection naming conventions in Mongoose.

Get Unlimited Access Now

Transcript from the "Exercise 7 Solution" Lesson

>> [MUSIC]

>> Speaker 1: Cool, so what that looks like is connect to the database. I'm just connecting to, connecting to todos. I was going to change it because the name. That's the name of the collection as well so I'll change it to something else like puppies. So we run this file.

[00:00:20] And let me try to clean this stuff up.
>> Speaker 1: All right, you'll say boom. You'll be like all right, this is what I created. And then what we can do is go over to our shell. I'm just gonna say use the name.
>> Speaker 1: And then now what happens is if you didn't hear me when I told you what collection it was, the way you would figure that out is if you were in this file and we had this mongoose.model, it takes two arguments.

[00:00:54] It was actually just a getter and a setter. So if you pass in a second argument it's assuming that you want to create a model. If you exclude the second argument It's assuming you are trying to reference a model. So it's a getter and a setter. So we're doing the setter.

[00:01:08] We're creating a model, and that way that works is the first argument is the name of the collection you want to make. It doesn't matter what name I put here, Mongoose is gonna lower case it and pluralize it. So if I put in, I want to create a TODO model, or a TODO collection, it's actually gonna a create a todos.

[00:01:30] It's gonna lower case and pluralize it, so I just put it in there like that. And then the second argument is the schema, which is the one we made up here. So we're going to talk about schema and this stuff on the next step, so if you don't know what this stuff means don't worry about it.

[00:01:44] We're going to talk about it, that's why I didn't ask you to write it. But that's how you know what collection it is. Because that's the name of it right here. And then we're creating one, and then we're logging it. So that means if we come back over here, and we say we switched our database.

[00:02:00] We say db dot and you can just say show collections too. It will show you the collections. There's todos right there, db.the name of the collection, which is todos.find. That will give you all of them. Should be two or three in here by now, as I restarted it.

[00:02:17] It's just one actually, so just one.
>> Speaker 1: There it is. And if you're using RoboMongo,
>> Speaker 1: You can come in here, go to your database, your collections, your,
>> Speaker 1: And it's right there.
>> Speaker 1: Sweet. So a little more on that conversion here. Even if we had something that ended in a y, it will IES it.

>> Speaker 2: Yeah, [INAUDIBLE] so we have-
>> Speaker 1: Puppy?
>> Speaker 2: Yeah, puppy.
>> Speaker 1: That will turn it into puppies. It's trippy, right? Yeah, so, remember that. To avoid that, I just try to avoid things with y, but we have a thing called category that has a y in it, so yay.

[00:03:33] But, we won't really be messing around with a lower level name with the collections in the driver, anyway. So, it's kind of irrelevant, but, just to let you know.