Transcript from the "Creating Associations with a Nested Schema" Lesson
>> Scott Moss: Let's talk about relations. So I had a question earlier about if I wanted to embed another document in a document, how would I do that? Were they basically like relationships? There's a few ways you could do that. One, you could literally just embed the document like this, right, in as an object, so it's just an object.
[00:00:23] But if you want this thing to have an ID where it's its own separate thing, what you can do is you can actually just add a schema in here as well. These are nested schemas. I highly recommend not doing that. I almost never do that in production. Instead what you probably wanna do, is you want another model, and a reference to it here.
[00:00:41] You wanna reference that model with something like an ID, or something like a foreign key, so that's something you probably wanna do. So I'm gonna show you how to do that. So what we're gonna do is, we're gonna make another schema here, or another model, and we'll gonna call it school.
>> Scott Moss: And school is just gonna be a new mongoose.schema.
>> Scott Moss: And right now, we'll just give the school a name. That's about it, and it'll just be of type String, it's not that important. So now we have a school, and then a student belongs to a school, so we have two options.
[00:01:19] If you've ever done any relational data building, or data configuration, then you know that we could store the students array on the school. Or we can store the school on the student, all right? It really depends on how you're trying to query this data. If you're gonna be reading from the school all the time, you might put the students on the school.
[00:01:41] But then you have an array that can be infinitely big, so you might have a problem. Or you might just keep it one-to-one and put the school on the student, and that way you're good to go. We're just gonna put the school on the student for now. So for here what we're gonna do is, let's make another field on the student, and we'll just call it school.
>> Scott Moss: And the first thing to do is we're gonna give it a type. Now if the type is going to be a reference to some other document somewhere, then we need that to be something reliable, something unique. By default, the most unique thing in the database is gonna be an ID, so that's the type that we're gonna use.
[00:02:19] So we wanna use the ObjectID type, which is just gonna be mongoose.Schema with a capital S, .Types with a captial T, .ObjectId. So that's the ObjectID type. This is saying this type is going to be the same ObjectID type that all of our underscore IDs are composed of inside of Mongo.
[00:02:42] This alone is not telling Mongo that this is referring to a school somewhere. It's just saying there's a field called school, and its type is an ID, that's all it's doing. We've still gotta do just a little more work here. This should be required, because all students should be in school, so this is required.
[00:03:01] Still, this doesn't tell Mongo and Mongoose that this school values belonging to an actual school model. So the last thing we wanna do, and we can do this here, we can do this at query time, there's a lot of places we can do it. But I think it makes more sense to do it on the schema, and that's to provide a reference.
[00:03:24] So the reference is going to be the collection in which this ID is gonna belong to. When we query for this later, Mongoose is automatically gonna go to that collection. Like yeah, I'm going to the collection that you told me to to find this matching ID. So what you can do is you can just put ref, and then we put the name of the collection.
[00:03:43] Now if we rewind back to what I said about naming collections, the collection name is gonna be whatever name that you pass into this first argument right here, mongoose.model. You don't have to think ahead of time and add the pluralized version, you can just put whatever you put here.
[00:03:58] So in this case the school one, which we haven't made yet, let's go ahead and make it. School =,
>> Scott Moss: mongoose.model, and we'll just use school, lowercase. And we'll put the school schema there, there we go. So school lowercase places school in there, we get a school model.
[00:04:31] So if that's gonna be the name of our collection for school, then the reference for the school filled on the student schema, that's just gonna be called school as well.
>> Scott Moss: So walking through this, this is saying on student.school, its type is gonna be an ObjectID. It is required.
[00:04:51] And when I ask for it Mongoose, I want you to match this ID with something in the school collection, that's what it's doing. We can point this to any collection, but we decided to do school. Any questions on this part?
>> Speaker 2: So that's how it tells it to save in separate areas, right?
[00:05:10] If you do the reference, right? Otherwise it would put it all in the same document, kind of?
>> Scott Moss: No, it won't save it in both collections for you, you still have to save it separately. So if you're asking if-
>> Speaker 2: It won't save it in the other collection?
>> Scott Moss: No, cuz it only exists in one collection. So when we start querying this, I'm gonna show you, it's all virtual. It's not like a joined table, or some table that you created that there is a virtual thing. You could imagine is going to create a table at query time, and grab it for you if it exists.
[00:05:46] But it's not saved in the database like that. A school is ever only going to exist in the school collection, and then the student's ever only gonna refer to it by an ID, that's it. And IDs never change, so you don't have to change the ID in the student.school, cuz IDs never change.
[00:06:02] And then if you create a new school, then you have a new student with that school ID. So you almost never have to change these things. It's only when you add some field that changes a lot where you have to update them, but in this case we don't.
[00:06:14] And that's cuz we're only using references that don't change. So we don't have to worry about figuring out how to update them, it should be good to go. Other than the fact that if you delete a school, and you're referring to something that's not there anymore, then yeah, you have to clean it up yourself.
[00:06:29] And you're gonna be doing that in one of the exercises.
>> Speaker 2: Okay.
>> Scott Moss: Yeah.