Transcript from the "Mongoose Schemas" Lesson
>> Scott Moss: So I'm gonna show you a little more exercises and then you're gonna jump into the exercise and try to get some of this stuff to work. So we talked about validation. Someone asked a question about that. We talked about how to do more fields. And then I haven't showed you how to create.
[00:00:13] So I'm gonna show you all those right quick, that way you can get a feel of what they feel like. I can leave this code up too, but then you're gonna go off and you're gonna do a lot of that yourself. So the first thing is, I wanna talk about a little deeper dive into how these schemas are created, and a little more different type of fields and stuff you can do.
[00:00:31] So for instance, if we wanted to add a validation to first name. What you can do, instead of putting a type here, you put an object. And then object, you can use the keyword, type and actually put the type like this.
>> Scott Moss: There's no difference than what I had before in this, because all I'm adding is a type filled, but now this allows me to add more meta information.
[00:00:54] So now I can say the first thing is actually required now. And I put require true. So now the first thing is required. So if I try to save a student without this firstName supplied, it mongoose, which is error out, it's like nope can't do it.
>> Scott Moss: So that's how you can do one validation here.
[00:01:14] Other thing I wanted to show you is that the sweet thing about mongoose that you can just do nested structures, right? So if I had a student here student here and then I had info property about the student, and maybe the info was the school that the student went to.
[00:01:31] And that has a type of string.
>> Scott Moss: And this was the shoe size of the student.. [LAUGH] I don't know why you need your student shoe sizes, but I'm just thinking about shoes right now. [LAUGH] And this could be a type: Number.
>> Scott Moss: And this could go on and on and on.
[00:01:54] You can nest this object as much as you want. I mean, you could set validations on these fields, and you could do all different types of things if you wanted. And array type. So, faveFoods for the students. You can add an array type here and you literally just add an array literal and then inside of there it's the same thing you've been doing.
[00:02:10] It's an object with the type and I could say string. So, this faveFoods is an array of strings.
>> Scott Moss: Yes.
>> Speaker 2: Follow to that, it's not completely the same thing It's just not to mongoose. So okay, what happened if you nest things like that inside and then you want to access them on their own, like schools and things?
>> Scott Moss: You mean if you want to query for them on their own?
>> Speaker 2: Yeah exactly, cuz somebody has school types, for example, and then you have it inside and then you have it outside. Is it easy to do?
>> Scott Moss: It's pretty easy to do, yeah. So the query is aware of how you set your schemas up.
[00:02:51] So you can do dot notation in the queries, and I could show you how those queries work. But yeah it's pretty simple to do. So there's nothing stopping me from saying, give me a student whose info dot school is blank. Yeah cool, so yeah, you can play around with this.
[00:03:06] There's a lot more things you can do here. I mean, like I said, you can go pretty deep with this stuff and get nested. So, just actually just give an example of that unique one, you can just put unique true here. And then now this is an index for unique, it's not a validation.
[00:03:21] Required is a validation, unique is an index, so there's a difference. If I put unique true here, now the first name is gonna be unique to the entire collection. So, there could be no student with two first names now. With the two same first names. That's what this does, just for this collection.
[00:03:36] If I had another collection called apples and then had a first name then they could have the same name, but for student collection no.