Introduction to MongoDB

Querying Associations with populate

Scott Moss

Scott Moss

Initialized
Introduction to MongoDB

Check out a free preview of the full Introduction to MongoDB course

The "Querying Associations with populate" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Scott demonstrates how to use populate to build virtual join tables.

Preview
Close

Transcript from the "Querying Associations with populate" Lesson

[00:00:00]
>> Scott Moss: Okay, so now I'm gonna show you how to query. So the first thing is, let's create the school. So we'll say school =
>> Scott Moss: Await, school.create. And school only has like a name so, Mlk Elementary and then we got that and the next thing is we're gonna create a student.

[00:00:27]
So we'll say a student = await student.create. And a student has a first name that's required, and a school that's required. It's the only required thing, so I'll do that. First name is, Trisha and school. So remember if we go back to look at school, it's type is gonna be mongoose.schema.type.objectid.

[00:00:59]
Where did we get that id? Well, we already created a school, and we know if we create something in Mongo we get an id property back. So we'll just use school.id here or better yet, just to be explicit, school._id and we're gonna talk about the differences between the .id and _id, but the short answer is virtualization.

[00:01:18]
You're gonna be doing virtualization later on in the course but to be explicit, you can just use _id.
>> Scott Moss: So now I save the student with the school and then now, if I were to query the student later on application and I want to get the school for it.

[00:01:35]
The way I would do that is, I would say awaits, students, I'm sorry capital Student.findById, see where these ID here is already have an ID, student.ID. And then subduing the exec right away I'm gonna use this handy method called populate. So, this is why mongoose is really cool, they have this thing called populate which are basically like virtual join tables.

[00:02:04]
So populate's gonna do if I don't pass it any arguments it's gonna go look at the schema for the student. It's gonna look at anything that has a type Object ID and a reference to a collection and it's gonna go query the database for those and, inject those values into the field itself.

[00:02:23]
So, instead of getting back a school field that's an ID, I'll get back a school field that is a school object.
>> Scott Moss: All right, so if I populate that and then I run exec.
>> Scott Moss: I should get back at school, or a student with the school so, let's check that out

[00:02:48]
>> Scott Moss: I'm sorry, yeah, I'm thinking of an older version of Mongoose. So you do have to pass in the actual field here by default so passing the school field here and then it will go populated, so sorry about that. So now if I run that again, you can see now it actually populates the school, there's an ID, there's the name of the school, along with the other properties that I told you that really aren't that important.

[00:03:12]
So that's how you would do associations and relationships in Mongo so you set up the schema, what the ref and the type of ObjectId required is up to you and then you just have to call populate with the field that you wanna populate. And then, it will go fetch it for you, if it finds it and attach it to that field It'll basically hydrate it.

[00:03:34]
It'll turn that ID into a full object instance if it finds it and then you'll get it back.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now