1 00:00:02,390 --> 00:00:08,900 So let's go from drawing to database action. I'll go to my blog database 2 00:00:08,990 --> 00:00:14,550 and there I said when I want to use two collections, let's start with the users collection, there 3 00:00:14,600 --> 00:00:15,720 let's say I insert 4 00:00:15,740 --> 00:00:19,900 many users, we get multiple authors on our blog. 5 00:00:20,130 --> 00:00:26,960 Now insertMany takes an array of documents where each document is one element we insert into this 6 00:00:26,960 --> 00:00:32,960 collection, a user here can have a name, Max Schwarzmuller, can have an age, 7 00:00:33,830 --> 00:00:41,010 29 and let's say an e-mail address, max@test.com which is not my real address. 8 00:00:41,050 --> 00:00:50,500 I also have a second author, Manuel Lorenz, age 30 and also an e-mail here, manu@test.com, if I hit enter, 9 00:00:50,540 --> 00:00:52,520 these two elements get inserted 10 00:00:52,720 --> 00:00:55,810 and obviously we can now pretty print them, 11 00:00:55,840 --> 00:00:58,510 this is the data in our users collection. 12 00:00:58,510 --> 00:01:01,660 Now let's go to our posts collection, 13 00:01:01,660 --> 00:01:03,390 so posts, there 14 00:01:03,400 --> 00:01:08,210 I want to insert one post let's say, obviously we could insert multiple posts. 15 00:01:08,500 --> 00:01:11,030 Now one post can have a title, 16 00:01:11,290 --> 00:01:19,210 my first post, a post can also have some text which holds the actual content, 17 00:01:19,410 --> 00:01:21,210 this is my first post, 18 00:01:21,210 --> 00:01:23,350 I hope you like it 19 00:01:23,670 --> 00:01:24,990 and this could be a very long text 20 00:01:24,990 --> 00:01:29,130 as you learned, the text type is able to store very long text. 21 00:01:29,220 --> 00:01:36,810 We might also have some tags here and that could be an array of strings like new tech, 22 00:01:36,820 --> 00:01:40,950 whatever your tags are and now we come to the relations. 23 00:01:41,080 --> 00:01:44,800 Now my relation here is that this post is written by a user, 24 00:01:44,810 --> 00:01:46,100 let's name it creator 25 00:01:46,210 --> 00:01:51,010 and this could be a list if you have multiple authors in your application, 26 00:01:51,010 --> 00:01:54,350 let's say in this application and that is totally up to you of course, 27 00:01:54,370 --> 00:01:57,580 we only have one author per host. 28 00:01:58,030 --> 00:02:03,740 So my creator or author, however you want to name it here should be one of my users 29 00:02:03,760 --> 00:02:08,170 and let's say this was written by Manuel, so I'll copy that objectId, 30 00:02:08,170 --> 00:02:11,850 we could match it by any key of course but we should insure it's unique 31 00:02:11,860 --> 00:02:19,760 and the objectId isn't sure to be unique and I'll therefore set this as a value for creator. 32 00:02:19,810 --> 00:02:27,310 Now we also might have comments here and let's say we get a couple of comments there and each comment 33 00:02:27,540 --> 00:02:28,980 then is another document, 34 00:02:28,990 --> 00:02:35,710 so here I got a list of documents where I have some text, 35 00:02:35,740 --> 00:02:40,970 I like this post and as another key here, 36 00:02:41,070 --> 00:02:46,500 I don't just have the text but also the author of this comment 37 00:02:46,640 --> 00:02:50,160 and that again would be the ID of a user, 38 00:02:50,390 --> 00:02:56,330 so let's grab Max here maybe, grab that objectId here and insert this 39 00:02:56,550 --> 00:02:57,530 as the author. 40 00:02:57,530 --> 00:02:58,470 So here again, 41 00:02:58,530 --> 00:03:04,640 I'm also using not an embedded user for the same reason why I'm not using it on a post, I'm not using 42 00:03:04,640 --> 00:03:05,890 it on a comment either 43 00:03:05,900 --> 00:03:08,260 instead here, I also use a reference. 44 00:03:08,350 --> 00:03:09,320 Let's hit enter, 45 00:03:09,320 --> 00:03:12,680 this worked and let's have a look at our post, 46 00:03:12,680 --> 00:03:13,890 this is how it looks like. 47 00:03:14,090 --> 00:03:15,600 And this post has the fields 48 00:03:15,770 --> 00:03:23,270 I sketched out and most importantly, we have a reference relation to the person who created the post 49 00:03:23,720 --> 00:03:30,290 but we got an embedded document or even multiple documents for the comments where each comment in turn 50 00:03:30,500 --> 00:03:34,310 also has a reference relation to the author. 51 00:03:34,790 --> 00:03:38,660 This is how our blog could look like in code, 52 00:03:38,690 --> 00:03:45,620 so in the database and I also explained why I would structure it like this and this example together with the 53 00:03:45,620 --> 00:03:52,790 previous lectures hopefully gets you into the right mental model on how to think about this and hopefully 54 00:03:52,790 --> 00:04:00,260 helps you with your next projects to structure your data and your relations in a way that makes sense 55 00:04:00,260 --> 00:04:03,260 for your application to your size of data, 56 00:04:03,260 --> 00:04:04,640 the amount of data, 57 00:04:04,640 --> 00:04:12,740 the frequency with which you change it and your overall structure or relation that is imposed on to you 58 00:04:12,950 --> 00:04:14,160 by your application.