0 1 00:00:00,180 --> 00:00:05,380 In the previous lessons, we looked at how we might implement CRUD using MongoDB. 1 2 00:00:05,430 --> 00:00:12,390 Now in this lesson, I want to show you how you might establish relationships in your database with MongoDB. 2 3 00:00:12,810 --> 00:00:15,020 There's two main ways of doing this 3 4 00:00:15,150 --> 00:00:18,730 and the first way is in most cases the preferred method. 4 5 00:00:18,990 --> 00:00:23,120 But what you choose will depend on the architecture of your data. 5 6 00:00:23,160 --> 00:00:27,270 So it depends on how your data relates to each other and how it's structured. 6 7 00:00:27,510 --> 00: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 8 00:00:34,350 --> 00:00:38,550 like db.products.insert 8 9 00:00:38,730 --> 00:00:44,130 and in this case the product that we want to insert happens to be a rubber. 9 10 00:00:44,490 --> 00:00:48,310 So we might say that this one has an id of 3 10 11 00:00:48,360 --> 00:00:54,320 and it's got a name of Rubber. 11 12 00:00:54,480 --> 00:01:01,340 It's got a price of 1.30 and a stock of 43. 12 13 00:01:01,920 --> 00:01:07,420 But this Rubber has also received reviews on our website. 13 14 00:01:07,770 --> 00:01:12,420 And so we can represent these reviews using an array. 14 15 00:01:12,780 --> 00:01:19,190 And inside the array we might have- we might have a few embedded documents. 15 16 00:01:19,530 --> 00:01:24,120 Every document is represented by a set of curly braces. 16 17 00:01:24,120 --> 00: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 18 00:01:31,380 --> 00:01:32,230 document. 18 19 00:01:32,520 --> 00:01:36,420 So our reviews document might have say an authorName 19 20 00:01:36,480 --> 00:01:42,410 so in this case it was somebody called Sally, and it might have a rating 20 21 00:01:42,600 --> 00:01:46,010 so they said that this was 5 stars. 21 22 00:01:46,710 --> 00:01:48,520 And then it might have a review 22 23 00:01:48,540 --> 00:01:49,720 right? 23 24 00:01:50,010 --> 00:01:54,120 Best rubber ever. 24 25 00:01:54,150 --> 00:02:01,720 OK so this would be a single document and because we're embedding it inside this particular product 25 26 00:02:02,050 --> 00:02:05,710 then we can associate the relationship in this way. 26 27 00:02:05,710 --> 00: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 28 00:02:11,890 --> 00:02:17,580 then we can simply append it into that array and we simply have some different data for it. 28 29 00:02:19,000 --> 00:02:27,380 And now we've established a relationship between a single product document and a single review document. 29 30 00:02:27,380 --> 00:02:35,600 Now this style of embedding documents in each other is really well-suited to this kind of one to many 30 31 00:02:35,690 --> 00:02:43,610 kind of relationship because one product might have many reviews or one user might have created many 31 32 00:02:43,640 --> 00:02:51,640 comments. And this is a very natural way of expressing that relationship using MongoDB. 32 33 00:02:51,800 --> 00:02:56,600 I'm going to copy and paste this into my Mongo shell. 33 34 00:02:56,600 --> 00: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 35 00:03:01,010 --> 00:03:03,980 see what's going on and inside the shell 35 36 00:03:04,010 --> 00:03:07,700 it's much harder to hit enter without executing your statement. 36 37 00:03:07,880 --> 00:03:11,970 So let's hit paste and hit enter. 37 38 00:03:12,130 --> 00: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 39 00:03:20,210 --> 00:03:26,330 a product with id 1 and a product with id 3. As a challenge 39 40 00:03:26,360 --> 00:03:30,470 I want you to practice embedding documents inside other documents. 40 41 00:03:30,680 --> 00:03:38,090 I want you to recreate that pencil document that we deleted earlier on and to add in some reviews for 41 42 00:03:38,090 --> 00:03:39,650 that document as well. 42 43 00:03:39,650 --> 00:03:46,760 The challenge is to follow the format that we created the last document and you're going to add two 43 44 00:03:46,760 --> 00:03:51,260 reviews to this pensil product which looks something like this 44 45 00:03:51,380 --> 00: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 46 00:03:58,010 --> 00:03:59,450 and also a review. 46 47 00:03:59,630 --> 00:04:01,860 Pause the video now and give that a go. 47 48 00:04:04,430 --> 00:04:04,820 OK. 48 49 00:04:04,840 --> 00:04:11,580 So I recommend writing this out inside a blank document in Atom just because it makes it so much easier 49 50 00:04:11,580 --> 00:04:13,310 to write multi-line code. 50 51 00:04:13,530 --> 00: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 52 00:04:18,600 --> 00: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 53 00:04:33,870 --> 00:04:37,680 cents, stock of 12 53 54 00:04:37,910 --> 00:04:45,200 and finally it's going to have that review field which is going to be an array of imbedded documents. 54 55 00:04:46,810 --> 00:04:50,810 Each embedded document will be enclosed in a set of curly braces 55 56 00:04:51,070 --> 00:04:53,970 and we're just going to make up some reviews right? 56 57 00:04:57,240 --> 00: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 58 00:05:04,020 --> 00:05:07,710 to our Mongo shell hit paste and hit enter. 58 59 00:05:07,740 --> 00: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 60 00:05:15,970 --> 00:05:19,300 in our products collection. 60 61 00:05:19,770 --> 00:05:28,490 Now another format you might see out there in the wild is you might say have two product documents, one 61 62 00:05:28,500 --> 00:05:35,370 which is the pen and the other which is the pencil and they each have a unique identifying id which 62 63 00:05:35,370 --> 00:05:37,840 is simply 1 and 2. 63 64 00:05:38,010 --> 00:05:43,200 And then you could create another collection say a collection of orders this time. 64 65 00:05:43,200 --> 00:05:49,080 And for each document in that collection you might have a order number but you might also have a field 65 66 00:05:49,080 --> 00:05:57,480 that's products ordered and this can simply be an array that references the id of the products in the 66 67 00:05:57,480 --> 00:05:59,060 products collection. 67 68 00:05:59,100 --> 00: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 69 00:06:06,120 --> 00:06:08,230 based off those IDs.