Well come back in this video going to show you how we can set up an association a one to many association
between two mongoose models by embedding data and we're going to be working with user and post.
So this will be like a Facebook or blog app where we have a user and a user can have many posts but
a post belongs to one user and we're not going to be creating an actual app here with routes and templates
.
We're just going to focus on the data and the data modeling.
So I'm going to get started by making a new file inside of this associations directory that I made.
You can do this wherever you want.
It's going to be a single file and I'll call it embed dot J.S. and I'll open that up and inside of here
I'll require mongoose which I haven't installed yet so I'll do that next and PM install mongoose and
that will take a moment.
There we go.
Let's make some space.
And once we require it the next thing we want to do is connect to a database so mongoose does connect
and this is totally up to us again the name of the database but it has to be Mangu D-B colon slash slash
local host slash and roll call.
This blog demo just like that and save.
OK so now we need to define two models we have user and we have a post.
The user will just have an e-mail and a name and a post.
It's going to have a title and then a content.
The actual post itself.
So we'll start with the user need to define the schema.
So far user schema equals new mongoose schema and then we're going to have email which is a string and
name which is a string just like that.
And then we'll create the model so far.
User equals mongoose model user.
The Cingular version and then user schema is what we're building it from.
And we'll save that.
OK.
Now let's move on to the post.
So we're going to create another schema this time var post schema equals new mongoose schema and then
each post has a title that is a string and content that will just make a string as well.
And then we also need to turn that into a model var post model equals mongoose stop model post comma
post schema.
Right.
So we have two schema set up two models.
Let's make a little bit of room here.
So we have our posts and we have our users.
And let's just try running this.
Make sure we don't have any syntax errors to start node embed Dutcher Yes.
Looks good.
Now let's go and create a single user just to make sure our model works.
So at the very bottom done here I'm going to make a new user so we'll just call it our new user equals
new user and then we'll pass an email and email will be Charlie at around Edu and name will be Charlie
Brown just like that.
And then what saves a new user to save.
And then we'll add in our callback with the error and then the data coming back which is hopefully our
user and then not just you.
If error cancels out log error.
Else we're going to cancel that log user.
So so far no associations.
I'm just testing to see if our user model works.
So let's run this node embed Dutchesse and we get our user back with the ID that was automatically generated
name and email.
Great let's try the same thing with post so comment that out so we don't get another user and we're
going to do our new post equals new post and a Post has a title and the title of the post will be reflections
on apples and the text of that post or the content.
I think we called it yes content will be.
They are delicious.
All right.
Great blog post there.
We'll save make sure everything's good and then we're going to actually do a new post that save us and
the callback again by now.
Hopefully you're getting comfortable with this pattern because you're seeing it nonstop where are passing
this call callback to mongoose.
Every time we do anything with Monga is finding creating saving updating eventually removing eventually
we need to pass in a callback with the error as the first parameter and then the data as the second
one.
All right.
So if error cancels out log ever otherwise come to that blog post make sure these both work.
Let's try it and we run into a small issue.
It's telling us that post is not defined when we're trying to make a new post.
And the most logical reason for that to happen would be if we named our model something other than post
.
And what do you know.
We named it post model and by we I mean that I named it incorrectly.
So let's go with post.
We could have kept it as post model and then changed it here.
But it's definitely conventional to have it just be the name of what you're making you don't need to
have model in there you don't need any extra characters just the name of the model itself or of the
entity post with a capital P..
All right so that matches now.
Right we have post and post here.
Let's try this again and this time it adds a new post content.
They are delicious title reflections on apples.
OK now that we've made sure that our two models are working correctly we can make a post and we can
make a user.
But currently they have nothing to do with one another and we want them to have a relationship remember
that we want a user to have or to own many posts and a post will belong to one user.
So that's a one to many relationship.
And the way that I'm going to show you to accomplish that is by embedding data instead of a user schema
.
So it's going to look like this.
Then I'll explain it in just a moment.
We're going to have a post's attribute inside the user and inside the Post's attribute.
We're going to have a bunch of posts and to make that work we need to add the post schema inside of
an array.
So rather than making posts a string or a number or a boolean we're telling mongoose we want it to be
a list an array of posts and we have to write post schema which is a little bit confusing honestly.
I wish that it was just post the name of the model but it's just the way that it is.
It needs to be the name of the schema and we have one other issue which is that if we run this right
now it's not going to know what post-chemo is and that's because we're defining it second.
So if we're embedding the data we need to define it first.
So we define the post schema and the post model and then we define the user schema and inside the user
schema we're adding an attribute called posts which is an array of posts.
So if we do that our data will look something like this or we'll have a name for a user let's do e-mail
and I'll just do jibberish a name and then we'll also have posts and posts an array.
And inside that array we have multiple posts so each post has a title and content and a user can have
many posts just like that.
And so for every post that we get it will be a new post instead of the posts attribute on a particular
user.
So let's get that going.
Let's create a user and then I'll show you how we can add a post to that user so we can just use our
.
Charlie Brown here.
New User equals Charlie Brown.
Although I'll change some of the e-mails and names because we already have Charlie Brown in the database
.
Let's go with are my any at Hogwarts edu.
And then we'll change this to be my auntie Granger.
All right.
So we have a new user and then to add a post to that user.
All they have to do is write new user posts that push because it's an array.
And I push in a new post and a Post has a title and the title of my next post will be how to brew poly
juice potion and then the content will be just kidding.
The potions class to learn it all right.
Whatever it doesn't really matter.
So we have a title and some content and then we're going to push this post into new user which we defined
up here.
And the last thing we need to do is save new user.
So a new user that save like that.
And we actually already have this written so I'll just reuse that and uncomment that and this will try
and save it.
If there's an error it will let us know.
Otherwise it will show us the user and then comment out all this stuff down here about posts and just
save that and let's see what happens.
So we have our post schema which you defined then we have the user schema and the new part is right
here where we have an array of posts inside of the user.
And then we're creating a new user and pushing into that post right.
Let's give it a shot.
OK.
And we get back this user that we're printing here emails reminding at Hogwarts name it's Hermione Granger
and then instead of posts we have a new post.
So that's working just fine.
Now let's try something else where I'm going to comment all this out and rather than creating a new
user let's retrieve the existing user.
So I'm going to retrieve her mining.
So I'll do that with user find.
And I'm going to use find one.
Otherwise find will give me an array and I just want one thing back.
So we're going to do you don't find one will find where name is Hermione Granger and then we'll do our
standard callback error and we'll just call this user.
Hopefully you're getting comfortable with that right now.
This right here the callback that we write pretty much every time we do anything with Mongoose and then
we'll just do it.
If ever
counsel dialog error and then Otherwise
we'll constantly bug user and I have an extra bracket I need to get rid of every go.
So this is just to find her my new back nothing new.
We're just finding a new user and it works.
We get the user coming back.
Her name is her mind she e-mails her money at Hogwarts.
OK now let's suppose I want to add another post because this is supposed to be a one to many.
So one user can have multiple posts.
Let's do that now.
So inside of the else once we find the user We're going to run some code to add in a new post and then
that looks like this user posts up push just like before and this time will push in another post so
we'll push in title and the title is three things.
I really hate and the content will be all the more all the more.
And Voldemort.
All right very creative post there remind me and we'll go ahead and save this.
So we still need to do a user save and then we'll add in our callback afterwards.
Another one of these error.
And we'll just call this user again.
And instead of here will do if error console log error otherwise cancel out log user.
All right so this is what some people would call callback.
Hell we have a lot of callbacks here.
We have this first one which is finding her mind the finding that user that we're getting and then we're
pushing in a new post.
But that doesn't actually add to the database.
We then have to save it.
And so we do user save and this user right here.
These two are referring to the user that comes back when we find her mind the the first time and then
we're saving and then this user is referring to what comes back from the save.
So this user here now has the new post added to it and save to the database.
It's the data that is coming back to us from Mongo and then we're printing it out.
Let's try it now.
So let me get rid of this first counterplot log.
Just leave it like that.
Let's clear make sure we don't have any other code running that's going to interfere with our accounts
about logs.
Let's run it.
All right.
So what we see is that we're now printing out a single user Hermione that has two posts as first one
and the second one and they're both stored inside of the post attribute on the single user.
So it's one to many.
OK.
So that's all I want to show.
Let's just reflect on this briefly.
So we started by sitting at Mongoose and we have two models and they were independent to start.
And then we associated them by adding this post schema array to the user schema.
So now a user has an array of posts and it starts empty.
But then we added to it by pushing in post dot push and then we saved in the next video going to show
you another way of associating data using what are called object references.