WEBVTT 0 00:00.180 --> 00:05.380 In the previous lessons, we looked at how we might implement CRUD using MongoDB. 1 00:05.430 --> 00:12.390 Now in this lesson, I want to show you how you might establish relationships in your database with MongoDB. 2 00:12.810 --> 00:15.020 There's two main ways of doing this 3 00:15.150 --> 00:18.730 and the first way is in most cases the preferred method. 4 00:18.990 --> 00:23.120 But what you choose will depend on the architecture of your data. 5 00:23.160 --> 00:27.270 So it depends on how your data relates to each other and how it's structured. 6 00:27.510 --> 00:34.350 Coming back to our shop database, let's say that we wanted to have another product so we might say something 7 00:34.350 --> 00:38.550 like db.products.insert 8 00:38.730 --> 00:44.130 and in this case the product that we want to insert happens to be a rubber. 9 00:44.490 --> 00:48.310 So we might say that this one has an id of 3 10 00:48.360 --> 00:54.320 and it's got a name of Rubber. 11 00:54.480 --> 01:01.340 It's got a price of 1.30 and a stock of 43. 12 01:01.920 --> 01:07.420 But this Rubber has also received reviews on our website. 13 01:07.770 --> 01:12.420 And so we can represent these reviews using an array. 14 01:12.780 --> 01:19.190 And inside the array we might have- we might have a few embedded documents. 15 01:19.530 --> 01:24.120 Every document is represented by a set of curly braces. 16 01:24.120 --> 01:31.380 And if we had a document that will represent the data for a review then we could embed that into another 17 01:31.380 --> 01:32.230 document. 18 01:32.520 --> 01:36.420 So our reviews document might have say an authorName 19 01:36.480 --> 01:42.410 so in this case it was somebody called Sally, and it might have a rating 20 01:42.600 --> 01:46.010 so they said that this was 5 stars. 21 01:46.710 --> 01:48.520 And then it might have a review 22 01:48.540 --> 01:49.720 right? 23 01:50.010 --> 01:54.120 Best rubber ever. 24 01:54.150 --> 02:01.720 OK so this would be a single document and because we're embedding it inside this particular product 25 02:02.050 --> 02:05.710 then we can associate the relationship in this way. 26 02:05.710 --> 02:11.560 And it's very very natural because it means that if we had another review come in for this rubber product 27 02:11.890 --> 02:17.580 then we can simply append it into that array and we simply have some different data for it. 28 02:19.000 --> 02:27.380 And now we've established a relationship between a single product document and a single review document. 29 02:27.380 --> 02:35.600 Now this style of embedding documents in each other is really well-suited to this kind of one to many 30 02:35.690 --> 02:43.610 kind of relationship because one product might have many reviews or one user might have created many 31 02:43.640 --> 02:51.640 comments. And this is a very natural way of expressing that relationship using MongoDB. 32 02:51.800 --> 02:56.600 I'm going to copy and paste this into my Mongo shell. 33 02:56.600 --> 03:01.010 Now the reason why I'm doing it inside Atom is just because it is much easier to format it for you to 34 03:01.010 --> 03:03.980 see what's going on and inside the shell 35 03:04.010 --> 03:07.700 it's much harder to hit enter without executing your statement. 36 03:07.880 --> 03:11.970 So let's hit paste and hit enter. 37 03:12.130 --> 03:20.090 And now I've inserted one document into my database and if I look through all of my products I now have 38 03:20.210 --> 03:26.330 a product with id 1 and a product with id 3. As a challenge 39 03:26.360 --> 03:30.470 I want you to practice embedding documents inside other documents. 40 03:30.680 --> 03:38.090 I want you to recreate that pencil document that we deleted earlier on and to add in some reviews for 41 03:38.090 --> 03:39.650 that document as well. 42 03:39.650 --> 03:46.760 The challenge is to follow the format that we created the last document and you're going to add two 43 03:46.760 --> 03:51.260 reviews to this pensil product which looks something like this 44 03:51.380 --> 03:57.920 but you're going to make up two reviews for the pencil and that each going to have a author name, a rating 45 03:58.010 --> 03:59.450 and also a review. 46 03:59.630 --> 04:01.860 Pause the video now and give that a go. 47 04:04.430 --> 04:04.820 OK. 48 04:04.840 --> 04:11.580 So I recommend writing this out inside a blank document in Atom just because it makes it so much easier 49 04:11.580 --> 04:13.310 to write multi-line code. 50 04:13.530 --> 04:18.550 So we're going to use db.products to tap into our products collection and then we're going to say insert 51 04:18.600 --> 04:33.870 One and the product that we're going to insert is going to have a id of 2, name of pencil, price of 80 52 04:33.870 --> 04:37.680 cents, stock of 12 53 04:37.910 --> 04:45.200 and finally it's going to have that review field which is going to be an array of imbedded documents. 54 04:46.810 --> 04:50.810 Each embedded document will be enclosed in a set of curly braces 55 04:51.070 --> 04:53.970 and we're just going to make up some reviews right? 56 04:57.240 --> 05:03.990 So now we can check that we don't have any errors in our code and we can copy the entire thing over 57 05:04.020 --> 05:07.710 to our Mongo shell hit paste and hit enter. 58 05:07.740 --> 05:15.580 And now we've inserted our second document. And now you can see we've got a pen, our pencil and our rubber 59 05:15.970 --> 05:19.300 in our products collection. 60 05:19.770 --> 05:28.490 Now another format you might see out there in the wild is you might say have two product documents, one 61 05:28.500 --> 05:35.370 which is the pen and the other which is the pencil and they each have a unique identifying id which 62 05:35.370 --> 05:37.840 is simply 1 and 2. 63 05:38.010 --> 05:43.200 And then you could create another collection say a collection of orders this time. 64 05:43.200 --> 05:49.080 And for each document in that collection you might have a order number but you might also have a field 65 05:49.080 --> 05:57.480 that's products ordered and this can simply be an array that references the id of the products in the 66 05:57.480 --> 05:59.060 products collection. 67 05:59.100 --> 06:06.060 And so at a later date you can pull up the product ordered and you can find which products they are 68 06:06.120 --> 06:08.230 based off those IDs.