1 00:00:00,650 --> 00:00:07,200 In the last section we started writing a query that would not only find but also update the appropriate 2 00:00:07,200 --> 00:00:09,390 survey in one entire ago. 3 00:00:09,480 --> 00:00:13,830 So we essentially will end up just running this operation right here. 4 00:00:13,830 --> 00:00:18,090 The correct surveys should be found and then correctly updated based on the criteria that we're going 5 00:00:18,090 --> 00:00:20,110 to pass in this second object right here. 6 00:00:20,140 --> 00:00:22,020 And so that's where we kind of left things off. 7 00:00:22,020 --> 00:00:27,240 We need to figure out exactly how we are going to update the appropriate survey like this one that we 8 00:00:27,240 --> 00:00:28,870 find right here. 9 00:00:29,280 --> 00:00:33,180 Now when the previous code that we were just looking at at the previous kind of iteration of this query 10 00:00:33,180 --> 00:00:34,820 that I was saying was really bad. 11 00:00:34,980 --> 00:00:38,970 You'll notice that there was essentially two big things that we wanted to do over here. 12 00:00:38,970 --> 00:00:44,450 The first thing we wanted to do was make sure that we updated the appropriate recipients responded propertied 13 00:00:44,480 --> 00:00:45,140 true. 14 00:00:45,390 --> 00:00:51,810 And then we wanted to increment the number of answers or the number of responses either the yes property 15 00:00:51,810 --> 00:00:54,410 or the no property by exactly one. 16 00:00:54,540 --> 00:00:57,980 And then we also set the last responded property as well. 17 00:00:58,290 --> 00:01:03,120 So right now let's just kind of forget this last responded property for just a minute because we didn't 18 00:01:03,120 --> 00:01:03,640 really care. 19 00:01:03,660 --> 00:01:06,060 You know this isn't the most critical property in the world. 20 00:01:06,090 --> 00:01:10,440 This was just a property that gave a little bit of information to our users to tell them hey here's 21 00:01:10,440 --> 00:01:13,160 the last time someone voted on a particular survey. 22 00:01:13,590 --> 00:01:16,310 So I want to just focus on these two properties right here. 23 00:01:16,310 --> 00:01:22,610 I just want to update the number of responses and I want to update the given recipient to have responded 24 00:01:22,620 --> 00:01:24,470 property of true. 25 00:01:24,620 --> 00:01:30,120 So back over at our query that we're in the process of writing I'm going to find the second object right 26 00:01:30,120 --> 00:01:35,250 here and inside of here we're going to place the logic that is going to update the respondent property 27 00:01:35,250 --> 00:01:39,930 on the recipient and to give an answer or then the given choice of yes or no. 28 00:01:39,930 --> 00:01:42,990 So let's take care of that choice property first. 29 00:01:42,990 --> 00:01:43,190 All right. 30 00:01:43,200 --> 00:01:44,700 Now this is going to be a little bit crazy. 31 00:01:44,730 --> 00:01:48,200 We're going to write out the code for it and we'll talk about exactly what it's doing. 32 00:01:48,690 --> 00:01:54,810 So inside of this second object I'm going to write dollar sign. 33 00:01:54,810 --> 00:02:05,160 I can see that's going to be an object that has brackets square brackets mind you choice Colan one like 34 00:02:05,160 --> 00:02:06,010 so. 35 00:02:06,630 --> 00:02:13,160 So triple check your typing make sure you've got exactly the same code right here. 36 00:02:13,180 --> 00:02:17,560 Now before we talk about what's going on I want to write up at the top remember that our choice property 37 00:02:17,620 --> 00:02:20,860 is always going to be either yes or no. 38 00:02:20,880 --> 00:02:26,260 So just remind our going to places up here just remind you what the choice property is going to be. 39 00:02:26,280 --> 00:02:26,510 All right. 40 00:02:26,510 --> 00:02:29,830 So let's talk about what is going on right here. 41 00:02:29,830 --> 00:02:34,580 This dollar sign I see thing is called a mongo operator. 42 00:02:34,690 --> 00:02:40,660 It allows us to put together some kind of intelligent or slightly intelligent logic inside of a query 43 00:02:41,650 --> 00:02:44,240 that we issue to our database. 44 00:02:44,260 --> 00:02:50,490 So this says find the choice property and remember remember choice is going to be either yes or no. 45 00:02:50,500 --> 00:02:55,730 We'll talk about what these spring spare rocket or square brackets excuse me I mean in just a second. 46 00:02:55,750 --> 00:03:04,390 So essentially find either the property or the no property and increment so I can see stands for increment 47 00:03:04,390 --> 00:03:06,430 it by exactly 1. 48 00:03:06,430 --> 00:03:13,600 So in other words provide one vote to either the yes or the no property on the given survey that was 49 00:03:13,600 --> 00:03:15,070 just found. 50 00:03:15,070 --> 00:03:20,800 Now of course we want to allow the decision of whether or not we are incrementing the yes or the no 51 00:03:20,800 --> 00:03:23,340 property to be determined on the fly. 52 00:03:23,680 --> 00:03:29,530 So in order to kind of calculate the key to use inside this object right here we're going to use a little 53 00:03:29,530 --> 00:03:34,360 bit of yes 20:16 syntax called qi interpellation. 54 00:03:34,420 --> 00:03:41,040 So if we put on here square brackets choice like so this does not create an array. 55 00:03:41,050 --> 00:03:42,180 Let me be really clear about that. 56 00:03:42,190 --> 00:03:44,050 It does not create an array. 57 00:03:44,440 --> 00:03:50,050 Instead when this object right here is the value evaluated by the javascript runtime it's going to say 58 00:03:50,350 --> 00:03:53,890 OK what's the value of the choice variable. 59 00:03:53,890 --> 00:03:59,760 If the value of choice is yes then it's going to replace this thing right here with yes. 60 00:03:59,800 --> 00:04:02,590 Otherwise if it's no it's going to replace it with no. 61 00:04:02,590 --> 00:04:07,230 So in other words we are saying we don't really know at the time that we are writing the code whether 62 00:04:07,250 --> 00:04:09,080 choice is going to be yes or no. 63 00:04:09,280 --> 00:04:14,470 So only when we are kind of trying to actually update the given survey only after we have an actual 64 00:04:14,470 --> 00:04:19,750 choice assigned will we actually figure out what this key right or should be in total. 65 00:04:19,750 --> 00:04:26,890 Again this little block right here says increment either yes or no by exactly 1 on the survey record 66 00:04:26,980 --> 00:04:28,050 that was found. 67 00:04:28,480 --> 00:04:28,740 OK. 68 00:04:28,760 --> 00:04:30,690 So that clearly takes care of step 1. 69 00:04:30,700 --> 00:04:35,210 We are incrementing the appropriate feedback property by one. 70 00:04:35,230 --> 00:04:37,630 Now the next thing we have to do is a little bit more complicated. 71 00:04:37,660 --> 00:04:43,030 Believe it or not the next thing we have to do is make sure that we take the recipient who has found 72 00:04:43,120 --> 00:04:46,420 and we update their responded property to True. 73 00:04:46,420 --> 00:04:50,050 So on this one we're just going to write out the code for applying this update and we'll talk about 74 00:04:50,050 --> 00:04:51,950 exactly what's going on. 75 00:04:51,970 --> 00:04:54,600 So after this dollar sign Inc. 76 00:04:54,610 --> 00:04:58,820 And this object direct here we'll put a comma and then the next line down. 77 00:04:58,820 --> 00:05:02,500 So this is another key value property inside the second object right here. 78 00:05:02,500 --> 00:05:09,490 We're going to say Dollar Sign set we're going to pass in an object. 79 00:05:09,950 --> 00:05:11,280 And this is where things get weird. 80 00:05:11,350 --> 00:05:21,460 We're you give it a key inside of a string of recipients dot dollars dollar sign dot responded are going 81 00:05:21,460 --> 00:05:24,060 to say that's going to be true like so. 82 00:05:24,640 --> 00:05:24,880 OK. 83 00:05:24,880 --> 00:05:30,580 So I think that in your head you understand more or less what this line is meant to accomplish. 84 00:05:30,580 --> 00:05:33,530 It is meant to update this responded property to True. 85 00:05:33,760 --> 00:05:38,320 But the real question is how does this kind of modifier actually work how does this actually update 86 00:05:38,320 --> 00:05:39,530 that property. 87 00:05:39,580 --> 00:05:43,150 So we're doing here is we are making use of another Mongo operator. 88 00:05:43,300 --> 00:05:49,930 We are saying that we want to set or update one of the properties inside of the survey record that was 89 00:05:49,930 --> 00:05:52,300 found by this initial query right here. 90 00:05:52,330 --> 00:05:59,950 So we are saying in the survey that was found look at the recipient's sub document collection inside 91 00:05:59,950 --> 00:06:01,420 that Subotnick collection. 92 00:06:01,450 --> 00:06:06,940 There are a bunch of different records to make sure that we are updating just the recipient that we 93 00:06:06,940 --> 00:06:08,030 really care about. 94 00:06:08,080 --> 00:06:15,880 We place dollar sign right here this dollar sign lines up with the dollar element or Elham match from 95 00:06:15,880 --> 00:06:17,780 the original query that was issued up here. 96 00:06:17,800 --> 00:06:22,660 So remember this line right here is going to find just the sub document collection recipient that we 97 00:06:22,660 --> 00:06:23,860 actually care about. 98 00:06:23,880 --> 00:06:29,280 And so maybe they are at index 500 inside of our list of recipients. 99 00:06:29,680 --> 00:06:35,290 So that 500 of an index is then used right here in place of the dollar sign. 100 00:06:35,650 --> 00:06:41,590 So you can imagine this thing is saying go into some documents collection find the appropriate recipient 101 00:06:41,650 --> 00:06:45,220 who was just found in the original query right here. 102 00:06:45,220 --> 00:06:52,650 Look at the respondent property and set that property to now be true rather than false like it was before. 103 00:06:52,960 --> 00:06:54,330 And that's pretty much it. 104 00:06:54,850 --> 00:06:58,210 So I know this clearly has we've written it out is really confusing. 105 00:06:58,210 --> 00:06:59,250 It's really crazy. 106 00:06:59,260 --> 00:07:04,600 But I want to give you a very intense a really in-depth query that you might find yourself writing when 107 00:07:04,810 --> 00:07:06,910 you are making use of Mongo. 108 00:07:06,910 --> 00:07:08,910 So let's walk through the entire thing again. 109 00:07:09,480 --> 00:07:16,740 We are saying look at the survey Compare can find an update exactly one record inside that collection 110 00:07:17,400 --> 00:07:25,140 we want to find the survey with this given ID who has a recipient with a given email and has not yet 111 00:07:25,140 --> 00:07:26,790 responded to the survey. 112 00:07:27,180 --> 00:07:35,100 After this one singular survey has been found make this following update to it increment the choice 113 00:07:35,190 --> 00:07:43,300 either yes or no by one and update the recipient who we just found in the original query update the 114 00:07:43,300 --> 00:07:48,900 respondent property to True because they have now actually responded so it's no longer responding false. 115 00:07:48,900 --> 00:07:50,550 It is now responded true. 116 00:07:51,100 --> 00:07:52,760 So that's very much the query interval. 117 00:07:53,070 --> 00:07:54,610 So this syntax right here. 118 00:07:54,610 --> 00:07:59,070 Yeah it's kind of nasty and you're probably sitting there thinking how in the world would I ever figure 119 00:07:59,070 --> 00:08:01,480 out how to write this query myself. 120 00:08:01,740 --> 00:08:04,770 And you know it's just something that comes with a little bit of experience. 121 00:08:04,770 --> 00:08:10,050 Again I'll give you at least a little bit of a toolset or just a very quick couple of tips on exactly 122 00:08:10,260 --> 00:08:13,870 how you can approach queries like this and figure out how to write them yourself. 123 00:08:14,290 --> 00:08:19,290 But the end of the day what I want you to really focus on is the fact that when we issue this query 124 00:08:19,290 --> 00:08:26,760 right here the entire thing from find the correct survey and then updating the survey is executed entirely 125 00:08:26,790 --> 00:08:28,750 inside of our Mongo database. 126 00:08:28,800 --> 00:08:35,160 So we are never going to exchange any information whatsoever from our express server back over to Mago 127 00:08:35,160 --> 00:08:35,570 DB. 128 00:08:35,580 --> 00:08:38,140 There's no communication there of the actual data. 129 00:08:38,340 --> 00:08:44,130 We are just sending one instruction over to Mongo and saying hey make this update happen like just do 130 00:08:44,130 --> 00:08:48,830 the thing Mungo's going to process this update and that's it. 131 00:08:48,840 --> 00:08:54,130 We never have to go through the very laborious process of fetching the record pulling over the whole 132 00:08:54,130 --> 00:08:56,150 as the recipients searching through them. 133 00:08:56,160 --> 00:09:02,340 Updating one recipient and then saving back the entire record which in reality is a very long running 134 00:09:02,340 --> 00:09:03,440 process. 135 00:09:03,850 --> 00:09:07,560 OK so I think that we probably railed on about this query long enough. 136 00:09:07,560 --> 00:09:10,660 I think that you probably understand a couple of the considerations around it. 137 00:09:10,950 --> 00:09:13,000 So at this point let's take a quick break. 138 00:09:13,020 --> 00:09:18,000 We're going to continue in the next section and when we do we're going to move this query that we just 139 00:09:18,000 --> 00:09:24,690 wrote right here back into our survey routes J.S. file and so we will make sure that for every event 140 00:09:24,780 --> 00:09:29,760 in our events array we attempt to actually run and apply this query right here. 141 00:09:29,760 --> 00:09:32,700 So quick break and I'll see you in just a minute.