In the last lesson we looked at how we would update and delete documents in our MongoDB database. 1
In this lesson I want to briefly look into how you might establish relationships between your documents 2
and embed documents into each other through the use of Mongoose. 3
We know that we currently have a single person in our people's collection who's called John and he has 4
an age of 37. 5
What if we could get John to have a favorite fruit which embeds a fruit document from our fruits collection? 6
Well the way that we would do this is we would have to establish a relationship. 7
Now we can't simply just call a person.updateOne and add in a brand new field that doesn't exist 8
in the schema because you can't have a new field that's not specified in the schema. 9
But what we can do though is we can add a new data entry, say for example if we were to create a new 10
person who now has a favorite fruit, inside the person's schema we're going to update this. 11
So we're going to change it so that we have a favorite fruit. 12
And this is going to have a data type of fruitSchema. 13
This tells Mongoose that we are embedding a fruit document inside this property called favoriteFruit 14
in our person document. 15
We can now create a new fruit and let's say it's a pineapple. 16
This is going to be a new fruit which has a name of 17
pineapple (piña) and it's got a score of 9 and a review of "Great fruit". 18
So now that we created this new fruit called Pineapple, let's go ahead and save it. And let's call pineapple 19
.save to save it into all fruits collection. 20
And then we're going to add this pineapple as an embedded document in a new person. And this person 21
is going to be called Amy and she's 12 and she has a favorite fruit. And that favorite fruit is of course 22
the pineapple document that we created up here. 23
Now let's uncomment person.not save to save Amy into our database of people and let's save and go over 24
to a hyper terminal and run node app.js. And let's go over to our Mongo shell. 25
So I want to show you firstly that if we go into db.people.find you can see that we have two 26
records. 27
One is John who's age 37 and he doesn't have a favorite fruit, but the other one is Amy and she's 12 28
but she has a favorite fruit and this is an embedded document. 29
So it's a fruit with a particular ID, a name of pineapple and a review. 30
The ID of this fruit is something something something 31
and it ends with a22. 32
If at this point you go into your fruits database and you look at everything that's in there, you can 33
see that the pineapple that's logged in here has exactly the same ID. 34
We now have this pineapple objects in the fruit collection that is linked to have a relationship with 35
a person in the people collection. So now as a challenge I want you to create a new fruit, to save that 36
fruit to the fruits collection, and to assign that fruit as the favorite fruit for John because even 37
though MongoDB allows us to have these asymmetrical data entries, for example this person doesn't 38
have a favorite fruit 39
but this one does because we updated the schema. 40
But let's give John a favorite fruit as well. 41
And in the process you'll get to create a new relationship. 42
Pause the video and give that ago. 43
Okay. 44
So let's first create our new fruit. 45
Let's say we're creating a Mango and it has a name of a Mango 46
and it has a score of whatever it is you want it to be 47
and it's also a pretty decent fruit. 48
So now we're going to save this mango into our fruits collection and we're going to instead of create 49
a new person, we're going to update our person namely John. 50
So we're going to call upon the person model which will tap into our people collection and we're going 51
to call the updateOne method. And we're going to pass in the condition that we want to find the document 52
that we want to update. 53
So in this case it's going to be a person with a name of John. And the update that we want to make is 54
we want to change his favorite fruit to now have a value and it's going to be a mango. Not a mongo 55
but a mango. 56
And finally we're going to add the callback to check if there was an error. 57
And it seems like despite my best efforts, I still spelt it as mongo. 58
So let's change that and try again. 59
Now it should have worked and it has successfully updated the document. 60
So now if we go and check out our fruits we now have a new mango fruit in there. 61
And if we check out our people, John now has a favorite fruit embedded into his record. 62
So this is an overview of how you can use MongoDB with Node.js by leveraging the simplicity and the 63
power of Mongoose. 64
Now in the next module we're going to be taking this one step further 65
and we're going to apply what we learned about Mongoose and MongoDB to our to do list to make it into 66
a real working application that's able to persist the data that we save into the to do lists. 67
So for all of that and more I'll see you on the next module.