1 00:00:00,760 --> 00:00:05,850 In the last section we put together a simple query to look through our survey collection and find all 2 00:00:05,850 --> 00:00:08,460 the surveys that were created by the current user. 3 00:00:08,460 --> 00:00:14,490 However we realized that if we stick with this query right here we'll also get the recipient list associated 4 00:00:14,490 --> 00:00:22,320 with every single survey record and that might have several tens hundreds thousands of email addresses 5 00:00:22,320 --> 00:00:23,600 associated with each one. 6 00:00:23,790 --> 00:00:29,220 And clearly our dashboard component inside of our application doesn't really care about that big list 7 00:00:29,220 --> 00:00:30,180 of emails. 8 00:00:30,210 --> 00:00:35,430 So I think that we can probably figure out some way to tell mongoose that when we go out to find this 9 00:00:35,460 --> 00:00:41,060 list of surveys do not return the recipient list and the response. 10 00:00:41,110 --> 00:00:41,410 OK. 11 00:00:41,460 --> 00:00:46,770 So we're going to look at the mongoose documentation to figure out exactly how to do this rather than 12 00:00:46,770 --> 00:00:48,480 have me just tell you how to do it. 13 00:00:48,480 --> 00:00:52,210 So I want you to get somewhat familiar with Mangus documentation. 14 00:00:52,560 --> 00:00:59,030 So going to change on over to my browser and in a new tab we're going to search for mongoose. 15 00:00:59,150 --> 00:01:01,780 J.S. Okay. 16 00:01:01,810 --> 00:01:03,640 So here's longers OTM. 17 00:01:03,670 --> 00:01:05,770 This is what we really want right here. 18 00:01:05,890 --> 00:01:08,410 So a click that here's the mongoose home page. 19 00:01:08,530 --> 00:01:11,580 We want to read the documentation. 20 00:01:11,780 --> 00:01:15,380 And then this pops us over to the documentation for mongoose. 21 00:01:15,380 --> 00:01:21,970 Now one quick note I want to throw out to you the documentation for Mongoose is really split into kind 22 00:01:21,970 --> 00:01:24,510 of like two separate locations. 23 00:01:24,560 --> 00:01:30,470 First on the left him Nav. over here you'll see this section called guide and underneath guide there 24 00:01:30,530 --> 00:01:36,590 is a lot of documentation about very particular subjects and each of these are written like little articles 25 00:01:36,840 --> 00:01:39,770 that you can check out and learn more about particular items. 26 00:01:39,950 --> 00:01:45,620 So if I click on validation it will give me some plain English definitions about how validation works 27 00:01:45,620 --> 00:01:47,080 with mongoose. 28 00:01:47,150 --> 00:01:51,630 I can also get a better idea of how say queries work right here. 29 00:01:51,680 --> 00:01:57,440 However if you want the real raw documentation that tells you about all the different particular methods 30 00:01:57,440 --> 00:02:02,590 and functions that you can make use of then you want to scroll down to the very bottom of the left 10 31 00:02:02,600 --> 00:02:10,370 Nav. over here and find the section called API docs so this is the kind of second location where all 32 00:02:10,370 --> 00:02:12,040 the documentation is concentrated. 33 00:02:12,050 --> 00:02:18,120 You've got that list of guides and then you've got this single one large API doc and I do mean large. 34 00:02:18,230 --> 00:02:23,170 For example if I start to scroll down you'll see the scroll bar over here yeah. 35 00:02:23,180 --> 00:02:30,290 It's a lot of stuff all on one page and kind of the bad side to that is it makes searching for documentation 36 00:02:30,290 --> 00:02:32,680 side if you're kind of challenging. 37 00:02:32,720 --> 00:02:38,120 There are some quick links down here on the left now that we can click on now that will tell you more 38 00:02:38,120 --> 00:02:41,970 about very specific areas inside of the mongoose library. 39 00:02:42,080 --> 00:02:44,910 But this kind of assumes you already know what you're looking for. 40 00:02:44,940 --> 00:02:49,610 And so for us in this particular case you know we don't really know what we're looking for. 41 00:02:49,790 --> 00:02:54,140 So it might be tough for us to find exactly the documentation that we want to look for. 42 00:02:54,200 --> 00:02:58,740 And so again remember that we just spoke about some tips and tricks around mongoose. 43 00:02:58,800 --> 00:03:04,910 Remember just about everything you could ever want to do around mongoose has a stack overflow question 44 00:03:04,910 --> 00:03:05,580 around it. 45 00:03:05,750 --> 00:03:11,120 So you can always do a nice quick search and say hey how do I do X Y Z with Mongoose and then hopefully 46 00:03:11,120 --> 00:03:16,370 that might give you some keywords to search for in the documentation. 47 00:03:16,370 --> 00:03:21,890 Now in this case I of course already done some research for us at a time and so I'm going to kind of 48 00:03:21,890 --> 00:03:27,410 point us in the right direction to find the documentation that we want to reference in order to figure 49 00:03:27,410 --> 00:03:31,970 out how we can tell mongooses not to return that list of recipients. 50 00:03:32,000 --> 00:03:38,650 So on this API doc page I'm going to hit command F or control f to bring up the search interface. 51 00:03:38,660 --> 00:03:46,050 Now we're going to do a search for pounde select and then you'll notice that there's six results here. 52 00:03:46,190 --> 00:03:53,140 So I'm going to keep searching until I find queery pound select right here OK. 53 00:03:53,160 --> 00:04:00,070 So if you've read the documentation says very simply which document fields to include or exclude and 54 00:04:00,070 --> 00:04:06,340 then very importantly you'll also see this kind of exclusion or inclusion of different fields on a record 55 00:04:06,640 --> 00:04:09,010 referred to as projection. 56 00:04:09,070 --> 00:04:13,840 You'll see that term sometimes in the mongoose or Mongo divi documentation. 57 00:04:13,840 --> 00:04:20,650 So the terms you know essentially this idea of including or excluding fields or white listing or blacklisting 58 00:04:20,650 --> 00:04:25,290 fields or creating a query projection they're all talking about the same thing. 59 00:04:25,330 --> 00:04:30,340 They're all saying when you go into the database and look for some data and pull it out. 60 00:04:30,610 --> 00:04:34,120 This is a set of fields that we want to have be returned. 61 00:04:34,360 --> 00:04:41,140 So essentially we're working with projections here so here is the documentation around how to do this. 62 00:04:41,220 --> 00:04:44,490 And the way to read this head right here. 63 00:04:44,490 --> 00:04:52,570 This says on the query class and then that pound right there indicates a high instance. 64 00:04:52,590 --> 00:04:56,280 So there is an instance method called select. 65 00:04:56,280 --> 00:05:01,040 So back inside of our application when we call survey done find right here. 66 00:05:01,290 --> 00:05:05,100 This returns a query object from Mungo's. 67 00:05:05,220 --> 00:05:12,060 And so after we do this initial query right here we can then train on as many additional aspects to 68 00:05:12,060 --> 00:05:13,470 the queries we want. 69 00:05:13,470 --> 00:05:20,130 So for example at the very end of this line or remove the semi-colon and then I can do a newline tab 70 00:05:20,130 --> 00:05:28,200 in and say Dot select like so and then I can start to customize the query that was created on this first 71 00:05:28,200 --> 00:05:29,360 line right here. 72 00:05:29,370 --> 00:05:30,250 Now just a quick note. 73 00:05:30,270 --> 00:05:33,310 We don't actually have to put this on a new line or indent it. 74 00:05:33,480 --> 00:05:38,160 It could just as easily go on the line right above it like so but usually just to make these queries 75 00:05:38,160 --> 00:05:39,500 a little bit more legible. 76 00:05:39,570 --> 00:05:45,130 Will New Line it and indent it so it reads a little bit more like plain English as OK go through alerts 77 00:05:45,250 --> 00:05:51,540 surveys find based on this criteria and then select these given properties inside of here. 78 00:05:52,010 --> 00:05:57,360 OK so that's kind of how we read this thing right here in how we make use of the Select function to 79 00:05:57,360 --> 00:06:00,080 pull off only specific properties. 80 00:06:00,380 --> 00:06:04,300 So let's scroll down a little bit more and get a better example of how to use this thing. 81 00:06:04,920 --> 00:06:05,220 OK. 82 00:06:05,250 --> 00:06:12,060 So basically on the query object or whatever query we create we then say does select and if we want 83 00:06:12,060 --> 00:06:19,410 to exclude specific fields we can pass in a string of dash and then the property name. 84 00:06:19,410 --> 00:06:25,550 Now here's something really interesting about mongoose that personally I find really really irritating. 85 00:06:25,560 --> 00:06:31,050 You'll notice that they give you one example right here of Oh yeah you can use a string to specify the 86 00:06:31,050 --> 00:06:37,170 property names you do or don't want but then they also give you this alternate syntax down here where 87 00:06:37,170 --> 00:06:44,130 they say oh but also you could just pass an object that specifies the properties that you want or the 88 00:06:44,130 --> 00:06:47,370 properties you don't want with either 1 or 0. 89 00:06:47,490 --> 00:06:53,910 And so to me it's kind of like you know why not just have one way or the other like either give us the 90 00:06:53,910 --> 00:06:56,700 string method or give us the object method. 91 00:06:56,700 --> 00:06:59,960 Otherwise it kind of just makes life a little bit unclear. 92 00:06:59,980 --> 00:07:05,400 And you know if you're an engineer and you've only ever learned the string method for including fields 93 00:07:05,660 --> 00:07:11,040 if you then see some code that uses an object you'll probably be sitting there saying like OK what's 94 00:07:11,040 --> 00:07:11,890 going on here. 95 00:07:12,120 --> 00:07:16,950 But you really know what's going on already is just this slightly different syntax that does the exact 96 00:07:16,950 --> 00:07:18,020 same thing. 97 00:07:18,210 --> 00:07:24,880 Anyways just one of my kind of pet peeves I guess I scroll in here or zoom in for one second because 98 00:07:24,900 --> 00:07:27,130 I want to point something out in particular. 99 00:07:27,420 --> 00:07:29,520 OK that's too much too much. 100 00:07:29,520 --> 00:07:30,540 Sorry. 101 00:07:30,810 --> 00:07:32,840 Let's go back to select. 102 00:07:32,950 --> 00:07:34,920 OK so here's what we are just looking at. 103 00:07:34,920 --> 00:07:39,270 So we're going to be going with this object syntax right here because I think it's just a little bit 104 00:07:39,270 --> 00:07:40,920 more obvious what's going on. 105 00:07:41,160 --> 00:07:46,920 But one thing I want to tell you is that the documentation says OK pass an object where the keys are 106 00:07:46,920 --> 00:07:51,180 the properties that you want to either specify to include or not include. 107 00:07:51,450 --> 00:07:58,980 And that shows a value of either 1 or 0 1 meaning include this property in the response or whatever 108 00:07:58,980 --> 00:08:03,260 gets pulled out of the database and a zero meaning don't include this property. 109 00:08:03,300 --> 00:08:06,550 So another kind of weird thing around the documentation here. 110 00:08:06,840 --> 00:08:12,430 These ones and zeroes are really just a shorthand representation of true or false. 111 00:08:12,660 --> 00:08:18,930 So you can either say a is one but you could just as easily say in like in the code we write you could 112 00:08:18,930 --> 00:08:20,940 say a is true. 113 00:08:20,940 --> 00:08:28,800 And I think using a true is way more clear what's going on there than saying select a of one like what 114 00:08:28,800 --> 00:08:29,940 does one mean. 115 00:08:29,940 --> 00:08:30,560 Right. 116 00:08:30,570 --> 00:08:35,160 I mean it's another item where if another engineer comes on the scene they're going to say select a 117 00:08:35,220 --> 00:08:41,610 of one why are we only finding results in this query where the property a is equal to one like that 118 00:08:41,610 --> 00:08:43,140 doesn't make any sense. 119 00:08:43,140 --> 00:08:46,770 So again another weird thing around the documentation here anyways. 120 00:08:46,860 --> 00:08:50,900 Hey that's enough complaining to me let's just do a select statement. 121 00:08:51,300 --> 00:08:51,520 OK. 122 00:08:51,530 --> 00:08:56,100 So back over inside of our code editor we're going to find the select clause on here. 123 00:08:56,250 --> 00:09:01,710 We're going to pass an object and then we're going to say specifically do not give us the list of recipients 124 00:09:02,160 --> 00:09:05,210 so we'll say recipients false. 125 00:09:05,310 --> 00:09:11,760 So do not include the recipient's property when we pull the list of surveys out of our database. 126 00:09:12,530 --> 00:09:12,950 OK. 127 00:09:12,960 --> 00:09:18,240 And I'll make sure that after the select statement I'll put on a semi colon and remember that on the 128 00:09:18,240 --> 00:09:23,910 line before it we should not have a semi-colon and it looks like pretty or wants to put everything on 129 00:09:23,910 --> 00:09:27,120 one line anyways whatever. 130 00:09:27,120 --> 00:09:27,470 OK. 131 00:09:27,480 --> 00:09:29,970 So I think that's about it for our API. 132 00:09:30,090 --> 00:09:34,150 Slash surveys end points we are making sure that the user is logged in. 133 00:09:34,170 --> 00:09:37,240 We specify that we're going to write some asynchronous code. 134 00:09:37,320 --> 00:09:41,900 We reach our database find all the surveys created by the current user. 135 00:09:42,260 --> 00:09:47,710 And then we specify that we do not want the result set to include the recipient's field. 136 00:09:47,750 --> 00:09:50,350 So I think that this is looking pretty good. 137 00:09:50,360 --> 00:09:55,280 Let's now continue in the next section where we will test out this point and we'll also get started 138 00:09:55,340 --> 00:09:57,620 on the front end aspect of this feature. 139 00:09:57,860 --> 00:10:00,310 So I'll see you in just a minute.