WEBVTT 0 00:00.480 --> 00:07.590 In the last lesson, we looked at how we would update and delete documents in our MongoDB database. 1 00:07.710 --> 00:14.100 In this lesson I want to briefly look into how you might establish relationships between your documents 2 00:14.310 --> 00:19.210 and embed documents into each other through the use of Mongoose. 3 00:19.230 --> 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 00:25.890 --> 00:27.750 an age of 37. 5 00:27.780 --> 00:36.650 What if we could get John to have a favorite fruit which embeds a fruit document from our fruits collection? 6 00:36.930 --> 00:42.850 Well the way that we would do this is we would have to establish a relationship. 7 00:42.910 --> 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 00:49.990 --> 00:56.330 in the schema because you can't have a new field that's not specified in the schema. 9 00:56.740 --> 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 01:03.760 --> 01:10.810 person who now has a favorite fruit, inside the person's schema we're going to update this. 11 01:10.870 --> 01:15.290 So we're going to change it so that we have a favorite fruit. 12 01:15.550 --> 01:20.590 And this is going to have a data type of fruitSchema. 13 01:20.590 --> 01:28.360 This tells Mongoose that we are embedding a fruit document inside this property called favoriteFruit 14 01:28.780 --> 01:31.360 in our person document. 15 01:31.420 --> 01:37.340 We can now create a new fruit and let's say it's a pineapple. 16 01:37.390 --> 01:42.990 This is going to be a new fruit which has a name of 17 01:45.740 --> 01:57.530 pineapple (piƱa) and it's got a score of 9 and a review of "Great fruit". 18 01:57.610 --> 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 02:03.700 --> 02:08.740 .save to save it into all fruits collection. 20 02:09.250 --> 02:16.210 And then we're going to add this pineapple as an embedded document in a new person. And this person 21 02:16.210 --> 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 02:25.570 --> 02:29.260 the pineapple document that we created up here. 23 02:29.560 --> 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 02:38.590 --> 02:45.090 to a hyper terminal and run node app.js. And let's go over to our Mongo shell. 25 02:45.130 --> 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 02:54.100 --> 02:54.980 records. 27 02:55.000 --> 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 03:03.580 --> 03:08.330 but she has a favorite fruit and this is an embedded document. 29 03:08.530 --> 03:15.250 So it's a fruit with a particular ID, a name of pineapple and a review. 30 03:15.640 --> 03:19.670 The ID of this fruit is something something something 31 03:19.690 --> 03:21.830 and it ends with a22. 32 03:22.210 --> 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 03:28.750 --> 03:36.200 see that the pineapple that's logged in here has exactly the same ID. 34 03:36.280 --> 03:44.530 We now have this pineapple objects in the fruit collection that is linked to have a relationship with 35 03:44.590 --> 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 03:54.690 --> 04:02.890 fruit to the fruits collection, and to assign that fruit as the favorite fruit for John because even 37 04:02.890 --> 04:09.310 though MongoDB allows us to have these asymmetrical data entries, for example this person doesn't 38 04:09.310 --> 04:10.190 have a favorite fruit 39 04:10.300 --> 04:13.400 but this one does because we updated the schema. 40 04:13.600 --> 04:15.930 But let's give John a favorite fruit as well. 41 04:15.970 --> 04:18.990 And in the process you'll get to create a new relationship. 42 04:19.060 --> 04:21.130 Pause the video and give that ago. 43 04:23.160 --> 04:23.610 Okay. 44 04:23.640 --> 04:26.700 So let's first create our new fruit. 45 04:26.940 --> 04:33.390 Let's say we're creating a Mango and it has a name of a Mango 46 04:33.930 --> 04:37.640 and it has a score of whatever it is you want it to be 47 04:37.680 --> 04:40.980 and it's also a pretty decent fruit. 48 04:41.070 --> 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 04:48.930 --> 04:54.490 a new person, we're going to update our person namely John. 50 04:54.570 --> 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 05:01.350 --> 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 05:09.240 --> 05:10.700 that we want to update. 53 05:10.710 --> 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 05:19.080 --> 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 05:28.680 --> 05:30.490 but a mango. 56 05:31.290 --> 05:36.860 And finally we're going to add the callback to check if there was an error. 57 05:44.200 --> 05:48.590 And it seems like despite my best efforts, I still spelt it as mongo. 58 05:48.820 --> 05:53.270 So let's change that and try again. 59 05:53.560 --> 05:58.170 Now it should have worked and it has successfully updated the document. 60 05:58.180 --> 06:04.350 So now if we go and check out our fruits we now have a new mango fruit in there. 61 06:04.660 --> 06:14.130 And if we check out our people, John now has a favorite fruit embedded into his record. 62 06:14.200 --> 06:21.250 So this is an overview of how you can use MongoDB with Node.js by leveraging the simplicity and the 63 06:21.250 --> 06:23.170 power of Mongoose. 64 06:23.380 --> 06:27.320 Now in the next module, we're going to be taking this one step further 65 06:27.580 --> 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 06:34.600 --> 06:40.950 a real working application that's able to persist the data that we save into the to do lists. 67 06:42.180 --> 06:44.950 So for all of that and more I'll see you on the next module.