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

Since Mongo is a NoSQL database, there are no join tables. Relational data instead uses Populations. Populations behave like a join table, but are used at call-time.

Get Unlimited Access Now

Transcript from the "Populations" Lesson

>> [MUSIC]

>> Speaker 1: Mongo is a NoSQL Database so we don't have join tables. I was talking about this earlier, but we need a way of seeing relational data, so the solution for this is called population. You can think of it as a join table at call time. So what does that look like, well imagine this we have a dogschema it has an owner, it's type is object id it refers to a person.

[00:00:28] Then we have a person schema it just has a name, so I make those two models, I make a dog model, I make a person model. All this we have done before. It's not new, and then what I wanna do is I say Dog.find which gives me all the dogs and I call this method called populate.

[00:00:49] And I pass in the property on the dogs that I want to populate. In this case I want the populate the owner fields, and then I call this method called exact. Exact, surface execute. It will either return a promise, which I'm using in this case, or it also takes a call back.

[00:01:07] A note style call back. So, it's up to you. You can pass it in note style callback here, which is function error, and result. Or you can use the promise, which is up to you. And if I'm looking at the dogs, which is an array, Each dog on there, it's owner property will not be an ID.

[00:01:25] It will be the object that the ID was representing from the personal collection. So what happens is when I call populate on this owner, it's gonna be okay, let me go to the owner field, let me grab the object ID, let me go to the person's collection, let me find the person with the object ID Grab that object and stick right here and stick it right here on owner.

[00:01:45] So now if you were to do dog dot owner it would be an object and the object would be the person that owns the dog. So we joined them for you. It populated the id field with the actual thing that it's referencing. That's why we have to put this ref here so it knows what collections to go match the ids with.

>> Speaker 2: How many times does the query Mongo for a statement like that?
>> Speaker 1: How many times does it query?
>> Speaker 2: Does it query once to get the dog collection and then does it query per dog filtered by owner?
>> Speaker 1: I don't know exactly how they're doing it, so I can't, I don't really want to count on that but, I think a naive solution would be doing once every time but I think they have a way around that.

[00:02:27] I am sure they are doing some optimization with their refs and object ids because they are indexed and now they know where they are. I don't think it will be that type of operation. Incredibly slow.
>> Speaker 2: Yeah.
>> Speaker 1: Yeah.
>> Speaker 1: So that's how you populate. If you wanted to populate many fields, so if we had another field up here that needed populating, we can just put a space after this word owner and put the field that we wanna populate there as well.

[00:02:55] It'll also populate that. Or you can also just do .populate again and call that. We also have fine grain control of what fields on the thing we're trying to populate, what do we want? So in this case, this is going to bring over the entire owner object. Well let's say, well I guess the owner only has a name, but the owner had like three properties on it, but all we wanted was the name.

[00:03:16] We could put a comma after owner and just be like just give me the name of the owner. So it would just send back an object with the owner's name and nothing else. So it's a little fine grain control.
>> Speaker 1: So again, there is so much to do and so many ways to do it with Mongoose and Mongo.

[00:03:35] Like I said, it's another chorus on its own. So I don't wanna get too deep into it, so I just showed you the basic things that you will be doing. So, this would be enough to get you started with the basic needs of our API. And again remember, because the API is restful, and it's all the same.

[00:03:48] They all have the same routes. If you write this queries once, with one model, it's the exact same thing for the other two models. It's a different model. It's the same operation. nothings different about it, except when you get into things like population for instance on the post model.

[00:04:06] This one's a little different because you have to make sure you populate these things when you call them whereas the other two don't have populations, they don't have any references. The user model has no references, the category model has no references, so they're pretty straight forward. This one, you just need to make sure you populate them.

[00:04:22] But they're still all pretty much the same. Find by ID, find, get that, get that.