0 1 00:00:00,480 --> 00:00:07,590 In the last lesson we looked at how we would update and delete documents in our MongoDB database. 1 2 00:00:07,710 --> 00:00:14,100 In this lesson I want to briefly look into how you might establish relationships between your documents 2 3 00:00:14,310 --> 00:00:19,210 and embed documents into each other through the use of Mongoose. 3 4 00:00:19,230 --> 00:00:25,890 We know that we currently have a single person in our people's collection who's called John and he has 4 5 00:00:25,890 --> 00:00:27,750 an age of 37. 5 6 00:00:27,780 --> 00:00:36,650 What if we could get John to have a favorite fruit which embeds a fruit document from our fruits collection? 6 7 00:00:36,930 --> 00:00:42,850 Well the way that we would do this is we would have to establish a relationship. 7 8 00:00:42,910 --> 00:00:49,900 Now we can't simply just call a person.updateOne and add in a brand new field that doesn't exist 8 9 00:00:49,990 --> 00:00:56,330 in the schema because you can't have a new field that's not specified in the schema. 9 10 00:00:56,740 --> 00:01:03,760 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 11 00:01:03,760 --> 00:01:10,810 person who now has a favorite fruit, inside the person's schema we're going to update this. 11 12 00:01:10,870 --> 00:01:15,290 So we're going to change it so that we have a favorite fruit. 12 13 00:01:15,550 --> 00:01:20,590 And this is going to have a data type of fruitSchema. 13 14 00:01:20,590 --> 00:01:28,360 This tells Mongoose that we are embedding a fruit document inside this property called favoriteFruit 14 15 00:01:28,780 --> 00:01:31,360 in our person document. 15 16 00:01:31,420 --> 00:01:37,340 We can now create a new fruit and let's say it's a pineapple. 16 17 00:01:37,390 --> 00:01:42,990 This is going to be a new fruit which has a name of 17 18 00:01:45,740 --> 00:01:57,530 pineapple (piƱa) and it's got a score of 9 and a review of "Great fruit". 18 19 00:01:57,610 --> 00:02:03,700 So now that we created this new fruit called Pineapple, let's go ahead and save it. And let's call pineapple 19 20 00:02:03,700 --> 00:02:08,740 .save to save it into all fruits collection. 20 21 00:02:09,250 --> 00:02:16,210 And then we're going to add this pineapple as an embedded document in a new person. And this person 21 22 00:02:16,210 --> 00:02:25,360 is going to be called Amy and she's 12 and she has a favorite fruit. And that favorite fruit is of course 22 23 00:02:25,570 --> 00:02:29,260 the pineapple document that we created up here. 23 24 00:02:29,560 --> 00:02:38,560 Now let's uncomment person.not save to save Amy into our database of people and let's save and go over 24 25 00:02:38,590 --> 00:02:45,090 to a hyper terminal and run node app.js. And let's go over to our Mongo shell. 25 26 00:02:45,130 --> 00:02:54,100 So I want to show you firstly that if we go into db.people.find you can see that we have two 26 27 00:02:54,100 --> 00:02:54,980 records. 27 28 00:02:55,000 --> 00:03:03,440 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 29 00:03:03,580 --> 00:03:08,330 but she has a favorite fruit and this is an embedded document. 29 30 00:03:08,530 --> 00:03:15,250 So it's a fruit with a particular ID, a name of pineapple and a review. 30 31 00:03:15,640 --> 00:03:19,670 The ID of this fruit is something something something 31 32 00:03:19,690 --> 00:03:21,830 and it ends with a22. 32 33 00:03:22,210 --> 00:03:28,750 If at this point you go into your fruits database and you look at everything that's in there, you can 33 34 00:03:28,750 --> 00:03:36,200 see that the pineapple that's logged in here has exactly the same ID. 34 35 00:03:36,280 --> 00:03:44,530 We now have this pineapple objects in the fruit collection that is linked to have a relationship with 35 36 00:03:44,590 --> 00:03:54,680 a person in the people collection. So now as a challenge I want you to create a new fruit, to save that 36 37 00:03:54,690 --> 00:04:02,890 fruit to the fruits collection, and to assign that fruit as the favorite fruit for John because even 37 38 00:04:02,890 --> 00:04:09,310 though MongoDB allows us to have these asymmetrical data entries, for example this person doesn't 38 39 00:04:09,310 --> 00:04:10,190 have a favorite fruit 39 40 00:04:10,300 --> 00:04:13,400 but this one does because we updated the schema. 40 41 00:04:13,600 --> 00:04:15,930 But let's give John a favorite fruit as well. 41 42 00:04:15,970 --> 00:04:18,990 And in the process you'll get to create a new relationship. 42 43 00:04:19,060 --> 00:04:21,130 Pause the video and give that ago. 43 44 00:04:23,160 --> 00:04:23,610 Okay. 44 45 00:04:23,640 --> 00:04:26,700 So let's first create our new fruit. 45 46 00:04:26,940 --> 00:04:33,390 Let's say we're creating a Mango and it has a name of a Mango 46 47 00:04:33,930 --> 00:04:37,640 and it has a score of whatever it is you want it to be 47 48 00:04:37,680 --> 00:04:40,980 and it's also a pretty decent fruit. 48 49 00:04:41,070 --> 00:04:48,840 So now we're going to save this mango into our fruits collection and we're going to instead of create 49 50 00:04:48,930 --> 00:04:54,490 a new person, we're going to update our person namely John. 50 51 00:04:54,570 --> 00:05:01,350 So we're going to call upon the person model which will tap into our people collection and we're going 51 52 00:05:01,350 --> 00:05:09,240 to call the updateOne method. And we're going to pass in the condition that we want to find the document 52 53 00:05:09,240 --> 00:05:10,700 that we want to update. 53 54 00:05:10,710 --> 00:05:19,080 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 55 00:05:19,080 --> 00:05:28,380 we want to change his favorite fruit to now have a value and it's going to be a mango. Not a mongo 55 56 00:05:28,680 --> 00:05:30,490 but a mango. 56 57 00:05:31,290 --> 00:05:36,860 And finally we're going to add the callback to check if there was an error. 57 58 00:05:44,200 --> 00:05:48,590 And it seems like despite my best efforts, I still spelt it as mongo. 58 59 00:05:48,820 --> 00:05:53,270 So let's change that and try again. 59 60 00:05:53,560 --> 00:05:58,170 Now it should have worked and it has successfully updated the document. 60 61 00:05:58,180 --> 00:06:04,350 So now if we go and check out our fruits we now have a new mango fruit in there. 61 62 00:06:04,660 --> 00:06:14,130 And if we check out our people, John now has a favorite fruit embedded into his record. 62 63 00:06:14,200 --> 00:06:21,250 So this is an overview of how you can use MongoDB with Node.js by leveraging the simplicity and the 63 64 00:06:21,250 --> 00:06:23,170 power of Mongoose. 64 65 00:06:23,380 --> 00:06:27,320 Now in the next module we're going to be taking this one step further 65 66 00:06:27,580 --> 00:06:34,420 and we're going to apply what we learned about Mongoose and MongoDB to our to do list to make it into 66 67 00:06:34,600 --> 00:06:40,950 a real working application that's able to persist the data that we save into the to do lists. 67 68 00:06:42,180 --> 00:06:44,950 So for all of that and more I'll see you on the next module.