Welcome back this video I'm going to show you the other way of associating data which is by using object
references.
So before we get started I'm going to make a new file here in the same directory and I'll call this
one.
References taught J.
And I'm going to just copy everything over into that file from our previous lesson and I'm going to
alter this.
The first thing I'll do is change the database that we use just so that we're not conflicting things
and we're not overwriting what we just previously did.
So I'm going to make the name blogg demo underscore too.
All right.
And then I'm going to go ahead and just get rid of a lot of stuff in here.
Pretty much everything aside from our schema that we have in the models.
And I'll start by talking about what it means to reference data versus embedding it.
So what we did last time in the last video was we embedded data where we had a user that looks something
like this with a name and there's an email in there as well.
And then there is a POST array and each post in that array was actually stored inside the array just
like this where title and content.
There we go.
And we have you know five six as many as we want and hear different posts inside the POST array.
Now what I'm going to show you in this lesson is very similar except what we actually do is have a poster
array and inside all that we're going to store in here rather than the actual posts.
We're going to have a bunch of IDs so we'll have these long IDs that look like that
and so on.
And these ideas will correspond to individual posts.
So then we'll have a post over here that has ID equal to this and it will have title you called something
else and so on.
So again the key difference is that we're storing IDs which is where the name reference comes from.
We're referencing the ID of another post rather than embedding the entire post.
OK so let's get started.
And we have to make a few changes to our schema rather than having posts be an array of post schema
.
What we actually do make it an array.
But we're going to pass an object in and the object is going to look like this.
I mean spaces out just a little bit.
And our object is going to have two main properties type and ref and type is going to be mongoose that
schema types dot object id and ref will be post.
So let's talk about what I just did here.
I've said that in the user schema there's a post attribute and it's an array but rather than being an
array of posts like we have here.
Or it's an array of the post schema.
It's actually an array of object IDs.
And this is just the way that we write that syntax.
It's a mongoose object id belonging to a post and we can save.
And just to make sure we didn't do anything wrong we can run the file make sure we don't get an error
.
Great.
Now let's get started by creating a single user that we can then add post to later.
So do user create and this one will give a email of Bob at gmail dot com and a name will be Bob Filcher
and we'll save and then we'll just leave it like that and we'll just assume that this is going to work
rather than adding the callback in.
So this is just going to create our first user.
So let's run the file.
All right.
So we don't see anything but we could go into Mangu and check it out.
So show Digby's use log demo underscore too and then we can do the users find and we have one user with
his name.
Bob Belcher email Bob at gmail dot com and posts that's empty because we haven't added any posts in
yet.
So let's get out of here and the next thing I want to do is add a post to this user so I'm going to
get rid of that create statement and I'll come out so that you can see it and what I'll do is make a
new post.
So post doc create and we're making it independently of the user.
So we're making that post and we'll give it a title of how to cook the best burger and then content
and not just fill it with blah blah blahs of laws for now just like that and will save and that should
make us a post and just to make sure this time let's add in the callback error and post and we'll just
cancel that log post and we're not going to handle the error just for the sake of time and run the file
.
And we've now added a new post to the database with an ID.
Remember this I.D. remember the whole thing 5 6 2 5 8 3 7 8.
Now I'm just kidding.
But but remember what it looks like.
And then we have our content and then our title.
All right.
So let's get out of there.
So what we want to do next is talk about how we can connect a post and a user using that ID.
And to do that I'm actually going to go through the steps of making another post.
So let's call this how to cook the best burger.
Part Two and this will make a new post and once we save that post We're then going to find the user
PABX.
So we're going to do a user find and we'll do find one and we'll find your name.
Let's do e-mail this time where e-mail is Bob at gmail dot com and then we'll run this code afterwards
ever and then we'll found that we'll call this found user.
OK.
So a lot going on.
So we're creating a post once the post has been created.
We're finding a user in this case this user here that we've already created and then we're going to
handle the error if we need to.
Otherwise we're then going to add the post into the users posts.
So it's found the user posts which is what we called it up here and we're going to push in push post
and post is referring to the post that we just created that was safe's the database with title How to
Cook the Vestberg or to content blah blah blah blah blah.
We save it if it works.
We have a post variable that storing the post and then refining the user.
In this case the one with email Bob at gmail dot com.
And then if we find it we're then going to add the new post into that users post array.
Then let's save that.
So afterwards we need to do another found user does save.
And yes we need a another callback and this one will have error and let's call this data just to make
it really clear what's happening.
And in here I'm just going to again handle the error
and then if there is no error going to cancel that log data.
All right man we have a lot here.
So create a post find the user and then take that post push it into the users posts save the user and
then when that's done print out the data.
Let's see what we get.
All right.
So it worked.
Here's our user name Bob Belcher email.
Here's the user id and then instead of posts all that we have is an array with one item and it's an
object ID versus when we had the embed J.S. where we were working with embedded data.
We had the entire post so that this array instead of it we actually saw the data.
Now we're just seeing the ID
so.
So let's go add one more.
So we'll do part 3.
And the text of that one instead of blah blah blah.
It's just going to be jibberish just so that it's easier to tell apart.
And the consul will add that post.
Now we have two object IDs here and each one corresponds to an individual post.
OK.
So that's how we can associate them.
But now we need to figure out how do we take this object id and find the correct post.
Let's say that we want to find a user.
And then I want to find that users post as well and not just the IDs but I want all of the data.
So I'm going to clear the console and I'm also going to comment out this behemoth here.
And so what we want to do is find user and then find all posts for that user.
And there's actually a way to do that in one go.
So we need to find the user first.
So that's user up find and we'll do find one again and then we need to pass and what we're looking for
which will be email where it's Bob at gmail dot com.
And then this is something new.
So we haven't seen this before.
Dot populate we're going to populate posts and then dot exec and I'll explain all this in just a moment
.
OK.
So what we've done here we've written a longer query that has multiple pieces.
So we're finding a user.
And then we're changing this populate posts which will actually populate the field posts look up all
those object IDs find the correct data and then stick it in the post array and then we run exec to actually
start the query.
So it's a little bit weird.
We're finding one.
But notice that we're not using a callback in here and we're changing up populate and then we're giving
it Dot exec at the very end which will actually make all of this happen.
So then we're passing callback into exec because that's when we're executing the code.
And if we did this right.
So let's check if there's an error.
First Council that log error.
Otherwise hopefully our user is going to have all the posts pre-populated inside the posts attribute
so will cancel that log user and let's try running this.
Make sure everything is commented out except for this.
Let's run.
And what do you know.
It worked.
So we get a user name is Bob Belsher email Bob at gmail dot com.
But now post isn't just an array of object IDs.
It actually has the full post in there.
Both of them.
So you can see it does have an idea what we have content and title content and title.
So to summarize everything that we did we store data that it looks like this here with object IDs.
You can see the two objects IDs and then we made a second query to retrieve the data and we filled in
the post Saray with all the data using populate that exec.
So you might be wondering why we would use one versus the other.
When would we embed data versus when would we use object id references.
And it's a really good and really important question that will address as we use them instead of our
camp.
The short answer is that it depends.
And you can get by using either style and pretty much every situation.
However there usually is one either using references or embedding data that is going to lend itself
better to whatever you're doing and we'll talk about that as we go through your camp and we start to
add in comments and users and we add in ratings and photos and all these different features.
Some of them are going to be embedded and some of them are going to be referenced