1 00:00:00,580 --> 00:00:05,340 In the last video we spoke about some document collections we had said that there is a very good reason 2 00:00:05,340 --> 00:00:09,390 for having a separate user collection and a separate survey collection. 3 00:00:09,390 --> 00:00:15,060 So we're now going to flip back over to our code editor and we're going to set up this recipient subcollection 4 00:00:15,300 --> 00:00:17,610 and nested within a single survey. 5 00:00:17,670 --> 00:00:23,310 So going to change back over to my code editor inside of my model's directory I'm going to make a new 6 00:00:23,310 --> 00:00:26,580 file and I'm going to call it recipient. 7 00:00:26,600 --> 00:00:33,810 J.S. now inside of here we are going to create our sub document collection by first creating a schema 8 00:00:33,810 --> 00:00:38,430 file or a schema object that represents a recipient. 9 00:00:38,430 --> 00:00:43,500 So remember that we had said that every single recipient should have two properties associated with 10 00:00:43,500 --> 00:00:43,930 it. 11 00:00:43,980 --> 00:00:49,200 They should have the email address and the click property clicked will be a boolean. 12 00:00:49,200 --> 00:00:54,930 That's either true or false depending upon whether or not this particular email address has received 13 00:00:55,290 --> 00:00:58,340 or has actually been used to click on a link previously. 14 00:00:58,350 --> 00:01:04,410 And remember this is all about somehow prigged preventing duplicate clicks on individual buttons by 15 00:01:04,410 --> 00:01:06,270 our users. 16 00:01:06,300 --> 00:01:06,550 OK. 17 00:01:06,570 --> 00:01:08,640 So back inside of recipient Yes. 18 00:01:08,700 --> 00:01:15,040 The initial setup of this schema is going to look very similar to how we previously set up models before. 19 00:01:15,120 --> 00:01:22,710 So we'll say Konst Mongoose is required mongoose will pull off the schema object 20 00:01:25,650 --> 00:01:32,980 and then we will use that schema object to make a recipient schema like so. 21 00:01:33,460 --> 00:01:38,620 Now again inside of here we really just want to reflect the user's email address and whether or not 22 00:01:38,620 --> 00:01:40,060 they've clicked on it before. 23 00:01:40,420 --> 00:01:46,740 So we're going to give it an email property of string and then a responded property. 24 00:01:46,760 --> 00:01:52,360 So I'm going to call those responded essentially you know clicked responded is obviously a lot more 25 00:01:52,360 --> 00:01:54,530 clear what it means rather than clicked. 26 00:01:54,760 --> 00:01:55,930 So responded means. 27 00:01:55,930 --> 00:01:58,210 Has this person responded to the survey yet. 28 00:01:58,270 --> 00:01:59,630 Yes or no. 29 00:01:59,650 --> 00:02:06,530 So we want this to be of type Boolean and we are going to default it's value to be false. 30 00:02:06,550 --> 00:02:11,820 So by default whenever surveys first created no they have not clicked on anything. 31 00:02:11,830 --> 00:02:19,370 And until you actually click on yes or no will we ever flip this respondent property over to true OK. 32 00:02:19,530 --> 00:02:25,190 So this is how we set up the initial definition of what our sub document looks like. 33 00:02:25,200 --> 00:02:29,940 So we're saying that we have a sub document as email and responded. 34 00:02:29,940 --> 00:02:33,380 And now at the bottom this is where things are going to be a little bit different. 35 00:02:33,450 --> 00:02:39,420 Rather than registering the schema with Mongoose we are going to export the schema. 36 00:02:39,420 --> 00:02:44,390 So I'll say module exports recipient schema. 37 00:02:44,820 --> 00:02:47,460 And again please check your spelling on recipients. 38 00:02:47,460 --> 00:02:49,240 Very easy to make a typo here. 39 00:02:50,550 --> 00:02:50,840 OK. 40 00:02:50,870 --> 00:02:54,070 So this now sets up the idea of a recipient. 41 00:02:54,110 --> 00:02:56,450 So we know what a recipient looks like. 42 00:02:56,570 --> 00:03:01,210 We are now going to import this schema into our survey. 43 00:03:01,220 --> 00:03:02,100 JS file. 44 00:03:02,150 --> 00:03:04,960 And then we will sociate it with the survey model. 45 00:03:05,340 --> 00:03:13,820 So we're going to change back over to survey geus at the top we will import recipient schema 46 00:03:16,300 --> 00:03:19,880 from dot slash recipient. 47 00:03:20,440 --> 00:03:25,660 And then finally we're going to use the recipient schema to set up the actual collection or some document 48 00:03:25,660 --> 00:03:27,340 collection inside of our model. 49 00:03:27,730 --> 00:03:30,110 So right here we still have the list of recipients. 50 00:03:30,130 --> 00:03:32,750 When we had said this was going to be an array of strings. 51 00:03:33,020 --> 00:03:38,560 Well now rather than being an array of strings we're going to say it's going to be an array or a list 52 00:03:38,890 --> 00:03:42,920 of records that conform to the recipient schema. 53 00:03:43,300 --> 00:03:50,290 So rather than saying recipients is an array of strings it's going to be an array of recipient schema 54 00:03:50,560 --> 00:03:51,690 like so. 55 00:03:52,300 --> 00:03:52,870 OK. 56 00:03:53,200 --> 00:04:00,520 So now whenever mongoose first loads up the survey's model whenever it ever saves a record to the survey's 57 00:04:00,520 --> 00:04:03,880 collection it will look at the schema right here. 58 00:04:03,940 --> 00:04:07,600 They'll say OK it's got a title it's got a body it's got to subject whatever. 59 00:04:07,660 --> 00:04:10,810 And then we'll get to recipients and we'll say Ooh interesting. 60 00:04:10,990 --> 00:04:15,840 This is going to be an array of recipient schema records. 61 00:04:16,120 --> 00:04:23,140 So every record or every object inside this array right here must obey the schema that we defined over 62 00:04:23,140 --> 00:04:24,410 here. 63 00:04:24,460 --> 00:04:25,820 So that's pretty much it. 64 00:04:25,840 --> 00:04:30,340 That's how we load up a sub or how we create a sub document collection. 65 00:04:30,340 --> 00:04:35,110 Now of course we still need to see how we add records to this sub document collection all that kind 66 00:04:35,110 --> 00:04:35,900 of good stuff. 67 00:04:35,980 --> 00:04:40,760 But this is at least good enough for understanding how we create it in the first place. 68 00:04:41,420 --> 00:04:41,770 OK. 69 00:04:41,800 --> 00:04:43,440 So this is looking pretty good. 70 00:04:43,450 --> 00:04:46,200 We've now got our sub document collection set up. 71 00:04:46,330 --> 00:04:48,730 Let's now continue in the next section.