1 00:00:00,450 --> 00:00:06,060 In the last video we added on the recipient's schema to set up the sub document collection. 2 00:00:06,060 --> 00:00:09,830 Now there's just one or two more properties that I want to add to the schema right here. 3 00:00:09,830 --> 00:00:15,660 Just while we're looking at it the first one is going to be a very necessary piece of code in a very 4 00:00:15,660 --> 00:00:18,150 necessary little addition to our schema. 5 00:00:18,150 --> 00:00:23,940 Remember that we've been saying up to this point that a user will have many surveys and so we need to 6 00:00:23,940 --> 00:00:28,540 somehow identify which survey belongs to which individual user. 7 00:00:28,590 --> 00:00:34,470 So to set up a relationship between our survey and between our user We're going to add another property 8 00:00:34,470 --> 00:00:36,850 to our survey schema object. 9 00:00:36,970 --> 00:00:41,610 So I'm going to go back over to my code editor and then down here at the bottom I'm going to add on 10 00:00:41,610 --> 00:00:49,830 one additional property I would call it underscore user then to make mongoose understand that this is 11 00:00:49,830 --> 00:00:56,370 going to be a reference to a very particular user or to a another instance of a user to make mongoose 12 00:00:56,370 --> 00:01:00,000 understand that we are setting up some type of relationship right here. 13 00:01:00,030 --> 00:01:08,730 We're going to assign an object to this property we're going to give it a type of schema types object 14 00:01:08,820 --> 00:01:18,300 id make sure that ID has a lowercase D and then we're going to give it also a ref property of user like 15 00:01:18,300 --> 00:01:21,280 so OK. 16 00:01:21,410 --> 00:01:25,690 So let's talk about this line right here a little bit with this property. 17 00:01:25,820 --> 00:01:32,900 We add the idea to survey schema that every survey is going to belong to a very particular user. 18 00:01:32,930 --> 00:01:38,030 First off indicate that it has a type of schema type object ID. 19 00:01:38,300 --> 00:01:43,250 So whenever this record whenever a schema actually gets saved to our database if we look up this user 20 00:01:43,250 --> 00:01:49,490 property we'll see an ID for assigned to this field right here and it will be the ID of the user who 21 00:01:49,490 --> 00:01:51,230 owns this record. 22 00:01:51,230 --> 00:01:55,310 Now notice we also add in reference here of user like so. 23 00:01:55,310 --> 00:02:00,890 So this tells mongoose that the reference that we're making this too belongs to the user's collection. 24 00:02:02,370 --> 00:02:07,140 Now the other thing I want to mention is that you'll notice that we prefixed this property name with 25 00:02:07,140 --> 00:02:10,250 an underscore here that is not at all required. 26 00:02:10,320 --> 00:02:17,160 So we could have just as easily called this thing user or it could have been owned by or it could have 27 00:02:17,160 --> 00:02:25,350 been I belong to absolutely anything we want but by convention we might make use of underscore user 28 00:02:25,350 --> 00:02:30,810 like this to make it obvious to anyone who's looking at this that this is supposed to be a relationship 29 00:02:30,810 --> 00:02:36,140 field or it's supposed to set up a relationship between this model and then and another one. 30 00:02:36,210 --> 00:02:41,550 So it's not necessarily some other silly property like all the other ones in here that contain raw data 31 00:02:41,880 --> 00:02:45,830 the underscore indicates that hey this is a reference field. 32 00:02:47,330 --> 00:02:47,720 OK. 33 00:02:47,810 --> 00:02:54,050 So that is going to be used to set up a relationship between a given schema and a given user. 34 00:02:54,050 --> 00:02:59,210 Now the very last two fields that I want to add in here and these are two fields that we haven't spoken 35 00:02:59,210 --> 00:03:04,460 about previously but I want to put on just to give you a little bit of an example of how you might kind 36 00:03:04,460 --> 00:03:07,250 of spice up the application a little bit. 37 00:03:07,250 --> 00:03:09,760 We're going to add on to additional fields here. 38 00:03:09,770 --> 00:03:19,070 The first one is going to be date scent of type date and then the next one will be last responded of 39 00:03:19,070 --> 00:03:21,540 type date as well. 40 00:03:21,650 --> 00:03:28,190 So the idea behind these as you might imagine any time we send out a survey via email will record the 41 00:03:28,190 --> 00:03:29,890 date that it was sent. 42 00:03:29,900 --> 00:03:35,540 We'll also make sure that we record the latest time that someone has voted on a given survey. 43 00:03:35,870 --> 00:03:40,730 So between these two properties right here we'll have the ability to give our users the idea of whether 44 00:03:40,730 --> 00:03:43,460 or not a survey is still kind of active. 45 00:03:43,520 --> 00:03:49,130 So we will be able to tell them hey here's when you sent the survey out and you've gotten like 500 responses 46 00:03:49,190 --> 00:03:54,170 in that time and the last time that someone responded was like a week ago. 47 00:03:54,170 --> 00:03:59,600 And so the last time you got a response was one or two weeks ago chances are you might not be really 48 00:03:59,600 --> 00:04:02,620 expecting to get a lot more feedback beyond that point. 49 00:04:02,630 --> 00:04:07,190 So these two fields right here are definitely not at all necessary for making our application work. 50 00:04:07,220 --> 00:04:13,330 There just a little bit of nicety to provide a little bit more feature to our users. 51 00:04:13,920 --> 00:04:14,410 OK. 52 00:04:14,480 --> 00:04:20,900 So we spent a lot of time on our survey schema and I think that it is now looking pretty good. 53 00:04:20,990 --> 00:04:24,450 So we've got our pretty core properties right here. 54 00:04:24,470 --> 00:04:29,660 We have embedded a list of recipients to make sure that we only ever let any given end user click on 55 00:04:29,660 --> 00:04:30,470 an e-mail link. 56 00:04:30,470 --> 00:04:36,770 One time we are recording the number of times someone votes or some of the total number of yeses and 57 00:04:36,770 --> 00:04:37,620 nos. 58 00:04:37,970 --> 00:04:42,310 We've got a relationship between the survey and a very particular user. 59 00:04:42,380 --> 00:04:46,370 And then we've got two additional fields in here to just give ourselves a little bit more information 60 00:04:46,430 --> 00:04:48,330 about this particular survey. 61 00:04:48,710 --> 00:04:50,630 So I think this is looking pretty good. 62 00:04:50,690 --> 00:04:56,490 I think that we are now ready to start putting together our endpoint to create and save a new schema. 63 00:04:56,750 --> 00:05:01,460 So let's continue in the next section and start putting together our new API endpoint.