1 00:00:00,210 --> 00:00:05,700 Now that we've seen the basics of the promised syntax we're gonna go ahead and explore how we can update 2 00:00:05,700 --> 00:00:07,940 documents using Mongo D.B.. 3 00:00:08,010 --> 00:00:11,630 And we're gonna do so with promises instead of callbacks. 4 00:00:11,670 --> 00:00:16,800 So to start I'm going to close both of the open files from the playground directory and I'm going to 5 00:00:16,800 --> 00:00:19,920 bring my tree view back into view from here. 6 00:00:19,920 --> 00:00:24,980 The only file we need access to is the one we've been using mostly in this section. 7 00:00:25,070 --> 00:00:28,530 Mongo D.B. dot J S now down below. 8 00:00:28,560 --> 00:00:31,700 We do want to switch the terminal directory as well. 9 00:00:31,740 --> 00:00:39,060 I'll use CDE dot dot forward slash task manager to switch over to that folder and I'll just use clear 10 00:00:39,060 --> 00:00:46,480 to clear the terminal output so we know how to create documents we can use insert one and insert many. 11 00:00:46,500 --> 00:00:50,650 We know how to read documents we can use find and find one. 12 00:00:50,730 --> 00:00:55,170 And for update we use UPDATE 1 and update many. 13 00:00:55,200 --> 00:00:57,780 Let's go ahead and explore them down below. 14 00:00:57,780 --> 00:01:04,500 I'm gonna remove all the code for find once again that's always available inside of the PD f guide for 15 00:01:04,500 --> 00:01:06,390 that lecture and down below. 16 00:01:06,390 --> 00:01:12,060 We're gonna start to update our documents to start let's go ahead and switch up someone's name. 17 00:01:12,330 --> 00:01:16,420 So inside of our database I have a collection of users. 18 00:01:16,470 --> 00:01:22,740 And right here as an example I have to Andrews I'm going to go ahead and adjust one of those to be something 19 00:01:22,800 --> 00:01:23,670 else. 20 00:01:23,670 --> 00:01:30,600 So what I'm going to do is target this second Andrew by I.D. and I'll update his name to something like 21 00:01:30,780 --> 00:01:32,840 Mike over in the API docs. 22 00:01:32,850 --> 00:01:39,030 We have two of the methods we'll be using for collection if we scroll down near the end for you we have 23 00:01:39,060 --> 00:01:44,150 Update 1 for updating one document and update many for updating multiple. 24 00:01:44,220 --> 00:01:47,340 There's also this update method that's been deprecated. 25 00:01:47,340 --> 00:01:52,090 It's an old way to update documents and it's going to be removed in a future version. 26 00:01:52,110 --> 00:01:57,240 You should either be using update many or update 1 depending on what you're trying to do. 27 00:01:57,300 --> 00:02:01,060 Now we'll start with UPDATE 1 to update just that one document. 28 00:02:01,200 --> 00:02:03,740 The first object we provide is the filter. 29 00:02:03,750 --> 00:02:09,510 So we target the one document we're trying to update we'll target it by I.D. so we could target it by 30 00:02:09,510 --> 00:02:11,370 any other field value. 31 00:02:11,370 --> 00:02:14,060 Then we have the updates we're trying to apply. 32 00:02:14,160 --> 00:02:19,740 Now there's also this optional options object which we're going to skip like we've been doing so far. 33 00:02:19,980 --> 00:02:23,520 And we have our callback as the third argument now for this. 34 00:02:23,520 --> 00:02:25,620 We're actually not going to use a callback. 35 00:02:25,620 --> 00:02:27,140 We're gonna use a promise. 36 00:02:27,150 --> 00:02:34,140 And down below I can see under its return value it returns a promise if no callback is passed in. 37 00:02:34,170 --> 00:02:39,810 Let's go ahead and actually use this first up I do need to grab the idea of the document I'm trying 38 00:02:39,810 --> 00:02:46,410 to update I'm gonna go ahead and right click this document to click edit so I can copy the I.D. value 39 00:02:46,710 --> 00:02:49,170 then I'll bring that over to my application code. 40 00:02:49,580 --> 00:02:55,710 So over here we're gonna go ahead and use Update 1 and to update just that one document. 41 00:02:55,710 --> 00:02:57,540 Now it still starts off these same way. 42 00:02:57,570 --> 00:03:04,620 So D.B. dot collection I'm trying to update a document in that user's collection and I'm gonna go ahead 43 00:03:04,650 --> 00:03:09,810 and use the update one method which we just explored. 44 00:03:09,850 --> 00:03:14,470 Now the first object this is where we provide the search criteria. 45 00:03:14,470 --> 00:03:22,870 So in our case we're going to target the document by its I.D. and the I.D. is going to equal a new object 46 00:03:22,900 --> 00:03:23,700 I.D.. 47 00:03:23,830 --> 00:03:31,120 And we're gonna go ahead and pass to it as its first and only argument the string value we copied from 48 00:03:31,150 --> 00:03:31,690 robo. 49 00:03:31,690 --> 00:03:32,680 3 T. 50 00:03:32,830 --> 00:03:37,050 So in this case we're creating an object I.D. using an existing value. 51 00:03:37,270 --> 00:03:41,390 And that's how we're gonna be able to target our documents accordingly. 52 00:03:41,530 --> 00:03:46,960 Now that we're targeting the document correctly we can go ahead and move on to the second argument for 53 00:03:46,960 --> 00:03:47,980 Update 1. 54 00:03:47,980 --> 00:03:50,580 This is the actual updates we want to apply. 55 00:03:50,920 --> 00:03:54,390 Now this is where things get a bit different from what you might expect. 56 00:03:54,400 --> 00:03:59,200 We don't just change things right here like setting name equal to Mike. 57 00:03:59,200 --> 00:04:05,380 Instead we use what are known as update operators to define the behavior we want to perform. 58 00:04:05,440 --> 00:04:07,920 We'll pull up the documentation for the update. 59 00:04:07,920 --> 00:04:09,200 Operators in a moment. 60 00:04:09,220 --> 00:04:13,740 Let's just start with the most common one which is dollar sign it's set. 61 00:04:13,740 --> 00:04:18,600 And this operator allows us to set new values for the fields in our document. 62 00:04:18,670 --> 00:04:24,760 And right here we're gonna go ahead and provide it an object value which is what it expects from here 63 00:04:24,760 --> 00:04:27,820 we can customize the fields like name or age. 64 00:04:27,820 --> 00:04:33,640 In this case I just want to customize the name of the document and I'll go ahead and set it to something 65 00:04:33,640 --> 00:04:33,990 new. 66 00:04:34,000 --> 00:04:36,850 That's not Andrew like Mike. 67 00:04:36,850 --> 00:04:37,920 Perfect. 68 00:04:37,930 --> 00:04:43,480 Now it's important to note that when we use set and we don't provide a field it's not going to delete 69 00:04:43,480 --> 00:04:43,680 it. 70 00:04:43,720 --> 00:04:48,430 In this case it's only impacting the fields we've explicitly listed out. 71 00:04:48,550 --> 00:04:50,470 Nothing's happening to age. 72 00:04:50,470 --> 00:04:57,310 Age is going to stay exactly as it is now if we were using the callback pattern we would go ahead and 73 00:04:57,310 --> 00:05:00,160 provide a third argument to update 1. 74 00:05:00,220 --> 00:05:05,890 This would be the function and we would get access to either the error or the result when we're using 75 00:05:05,890 --> 00:05:08,530 promises though we don't provide that function. 76 00:05:08,530 --> 00:05:13,330 Instead what Update 1 returns is indeed the promise. 77 00:05:13,330 --> 00:05:21,100 So right here we can create a new variable update promise as an example and down below we can go ahead 78 00:05:21,100 --> 00:05:24,700 and use some of the methods available on it right here. 79 00:05:24,730 --> 00:05:32,020 That would be update promise then to register a function to run when things go well and the document 80 00:05:32,110 --> 00:05:38,830 is updated and then down below we can set up a call to catch to register a function for when things 81 00:05:38,830 --> 00:05:39,660 fail. 82 00:05:39,760 --> 00:05:44,950 Now for the success case when the promise is fulfilled we'll get access to the result. 83 00:05:45,100 --> 00:05:50,290 And when the promise is rejected we'll get access to the error and for both we'll just log them to the 84 00:05:50,290 --> 00:05:55,000 terminal consult that log printing the result up above then down below. 85 00:05:55,000 --> 00:05:58,530 The same thing console dot log printing the error. 86 00:05:58,750 --> 00:05:59,800 Excellent. 87 00:05:59,800 --> 00:06:05,450 Now when we run this we're actually going to see the name field update over in robo 3. 88 00:06:05,470 --> 00:06:10,900 If I give things a quick refresh I can see the name still is Andrew down below. 89 00:06:10,900 --> 00:06:16,480 I'm gonna run the script node Mongo D.B. dot J. 90 00:06:16,480 --> 00:06:19,140 S to fire things off right here. 91 00:06:19,140 --> 00:06:23,700 We got a bunch of output and then over inside of robo three T. 92 00:06:23,790 --> 00:06:30,600 If we take things another refresh right here I can see the name has been updated to Mike so the output 93 00:06:30,600 --> 00:06:34,130 we're getting is all of the things available on result. 94 00:06:34,260 --> 00:06:41,490 And the only field we would ever pull off of there is modified count or possibly matched count matched 95 00:06:41,490 --> 00:06:45,450 Count is always going to be 1 or 0 for our update. 96 00:06:45,460 --> 00:06:53,520 One calls and modified count is going to be 1 or 0 1 if a document was modified and 0. 97 00:06:53,520 --> 00:06:57,100 If no document was modified so that's how we can use. 98 00:06:57,120 --> 00:06:59,520 Update 1 to update a document. 99 00:06:59,520 --> 00:07:02,190 Now we can also change this syntax. 100 00:07:02,190 --> 00:07:09,510 What we don't have to do is create a variable update promise and then add those method calls on there. 101 00:07:09,510 --> 00:07:11,700 We can actually change all of this together. 102 00:07:11,730 --> 00:07:12,980 So what does that mean. 103 00:07:12,990 --> 00:07:20,580 It means this variable can be deleted and down below we can add our dot then call right on to the end 104 00:07:20,670 --> 00:07:23,700 of the closing parentheses for update 1. 105 00:07:23,700 --> 00:07:26,130 So it's D.B. dot collection Dot. 106 00:07:26,130 --> 00:07:29,420 UPDATE One dot then dot catch. 107 00:07:29,540 --> 00:07:31,400 And this is a very common pattern. 108 00:07:31,470 --> 00:07:36,810 We're going to see throughout the class and that you're going to see as you start to work with a synchronous 109 00:07:36,840 --> 00:07:42,320 javascript whether that's on the front end or whether that's inside of node j. 110 00:07:42,350 --> 00:07:45,240 As this is a very common promise pattern. 111 00:07:45,300 --> 00:07:50,330 Now let's pull up the complete list of update operators so we can see what we have available to us. 112 00:07:50,340 --> 00:07:56,280 Aside from just set what I'm going to do is head over to the browser and what we're looking for is actually 113 00:07:56,280 --> 00:08:03,070 not part of the API documentation but we can crack open a new browser tab to find it right here. 114 00:08:03,090 --> 00:08:08,550 What I'm gonna do is Google Mongo D.B. update operators. 115 00:08:08,550 --> 00:08:13,440 This is going to bring us over to the correct article on the reference documentation. 116 00:08:13,500 --> 00:08:16,440 Right here we have the list of update operators. 117 00:08:16,560 --> 00:08:22,470 And if we scroll down a bit on this page it's going to bring us to a nice long list of operators which 118 00:08:22,470 --> 00:08:24,420 start with the dollar sign. 119 00:08:24,420 --> 00:08:29,430 So down below we have set which we already used and we have other nice ones as well. 120 00:08:29,430 --> 00:08:36,140 For example unset to remove a specific field from a document or rename to rename a field. 121 00:08:36,180 --> 00:08:42,120 We also have other ones that aren't quite as common including things like increment or ANC for short 122 00:08:42,420 --> 00:08:46,180 which allows you to increment a number in a particular document. 123 00:08:46,200 --> 00:08:50,080 We could use this as an example to increment the age. 124 00:08:50,100 --> 00:08:55,200 So these are some of the various ways we can work with our documents though in almost all cases you'll 125 00:08:55,200 --> 00:08:56,390 be using set. 126 00:08:56,460 --> 00:09:01,080 If you're trying to change the field value which is the most common scenario. 127 00:09:01,230 --> 00:09:03,560 The second most common would be to use increment. 128 00:09:03,630 --> 00:09:05,750 So let's go ahead and explore that. 129 00:09:05,880 --> 00:09:12,000 If we had over two robo three Ts we can see that Mike has an age of twenty seven. 130 00:09:12,000 --> 00:09:16,780 Let's go ahead and increment that to 28 from the visual studio code. 131 00:09:16,780 --> 00:09:23,680 Ed I'm gonna remove the set operator and we are going to use I N C instead. 132 00:09:23,680 --> 00:09:30,330 Now we can actually click the INEC operator in the documentation to learn how it works if we click on 133 00:09:30,330 --> 00:09:33,090 that it brings us over to some examples. 134 00:09:33,180 --> 00:09:38,000 Here we can see that we provide the field and the amount we want to increment by. 135 00:09:38,010 --> 00:09:39,440 So let's get that done. 136 00:09:39,480 --> 00:09:45,720 Now the only field we can actually increment is the age and we can increment in a positive way by using 137 00:09:45,750 --> 00:09:46,970 a positive number. 138 00:09:47,100 --> 00:09:50,200 Or we can decrement by using a negative number. 139 00:09:50,310 --> 00:09:54,380 In this case let's use one to increment the age by 1. 140 00:09:54,480 --> 00:09:56,550 I'm going to save Mongo DB. 141 00:09:56,640 --> 00:10:00,870 J S I'm going to rerun the script from the terminal down below. 142 00:10:00,960 --> 00:10:07,830 And if we had over two robo three TI we should see that age changed right here it was twenty seven I 143 00:10:07,830 --> 00:10:10,750 refresh things and it's now twenty eight. 144 00:10:10,860 --> 00:10:16,260 So those update operators allow us to do a lot of interesting things without needing to do more heavy 145 00:10:16,260 --> 00:10:22,260 lifting if we didn't have access to this operator we would have to read the document out of the database 146 00:10:22,440 --> 00:10:28,830 figure out what the current age is increment it and then use another operation to update the document. 147 00:10:28,890 --> 00:10:33,180 So that would be a fetch and an update with the increment operator. 148 00:10:33,180 --> 00:10:38,910 We can do all of this at once saving resources and getting things done just a bit quicker. 149 00:10:38,910 --> 00:10:44,790 Now it's challenge time it's going to be up to you to use update many which works very similarly to 150 00:10:44,790 --> 00:10:45,690 update 1. 151 00:10:45,740 --> 00:10:50,200 So instead of updating one it updates many as the name suggests. 152 00:10:50,220 --> 00:10:54,100 So I'm going to comment out all of the code for our update one call. 153 00:10:54,150 --> 00:10:59,190 So it doesn't keep incrementing that age though I'll leave it in place as a reference for the moment 154 00:10:59,490 --> 00:11:00,210 and down below. 155 00:11:00,210 --> 00:11:02,690 I'll paste in those challenged comments. 156 00:11:02,760 --> 00:11:08,030 So your goal is to use update many to complete all of the tasks. 157 00:11:08,130 --> 00:11:11,690 So we have our tasks collection over in robo 3D. 158 00:11:11,880 --> 00:11:15,730 I'll give things a quick refresh to make sure we're looking at the latest data. 159 00:11:15,730 --> 00:11:17,430 I have three tasks. 160 00:11:17,530 --> 00:11:25,240 One is complete the other two are not we want to use the update many operation to update all documents 161 00:11:25,240 --> 00:11:31,000 to be complete so when they're done I should see completed it true for everything. 162 00:11:31,000 --> 00:11:35,670 Now we can do that in one call with just update many as opposed to multiple with. 163 00:11:35,680 --> 00:11:36,440 Update 1. 164 00:11:36,670 --> 00:11:39,610 And that's exactly what you're going to do as your challenge. 165 00:11:39,610 --> 00:11:44,650 So step 1 Check out the documentation for update many to get comfortable with how it works. 166 00:11:44,650 --> 00:11:49,930 Hint It's kind of be pretty familiar to what we just learned about with Update 1. 167 00:11:50,050 --> 00:11:56,680 Then you're going to set up a call with the correct query and your updates using the necessary update. 168 00:11:56,680 --> 00:12:02,620 Operator And then you're going to use the promised methods to set up these success and error handler 169 00:12:02,620 --> 00:12:03,460 functions. 170 00:12:03,520 --> 00:12:05,640 And finally you're going to test your work. 171 00:12:05,770 --> 00:12:13,150 You should be able to run the file just a single time refresh things over in robo 3 T and see completed 172 00:12:13,210 --> 00:12:14,920 true for everything. 173 00:12:14,920 --> 00:12:20,410 Now with all of your tasks are already completed or you mess up and you need to get better sample data 174 00:12:20,620 --> 00:12:25,780 you can just right click one of your tasks edit it and switch the completed value over to false and 175 00:12:25,780 --> 00:12:28,240 rerun your script to make sure it's working. 176 00:12:28,450 --> 00:12:33,880 All right take some time to knock that out test your work and when you're done come back and click play 177 00:12:37,000 --> 00:12:37,750 how'd that go. 178 00:12:37,780 --> 00:12:39,960 Let's kick things off with step number one. 179 00:12:39,970 --> 00:12:42,270 Check out the documentation for update. 180 00:12:42,270 --> 00:12:48,190 Many now it's not a method we've used before but it is similar to methods we've used before and the 181 00:12:48,190 --> 00:12:53,980 goal of working through this section and through these challenges is not to talk about every single 182 00:12:53,980 --> 00:12:59,560 thing that Mongo D.B. can do because the course would become two hundred hours long and most of it would 183 00:12:59,560 --> 00:13:01,240 be terribly boring. 184 00:13:01,240 --> 00:13:07,000 The goal here is to give you the stuff that's essential to creating real world applications and to give 185 00:13:07,000 --> 00:13:09,970 you the tools and know how to explore the other things. 186 00:13:10,030 --> 00:13:13,480 When you're ready so down below we can go over to the update. 187 00:13:13,480 --> 00:13:17,780 Many docs and we can see that the first argument is our filter. 188 00:13:17,790 --> 00:13:19,690 The second is our updates. 189 00:13:19,690 --> 00:13:21,870 Then we have the options which we don't need. 190 00:13:21,880 --> 00:13:26,980 And we have our callback which we're not going to provide because we get a promise back if no callback 191 00:13:27,130 --> 00:13:28,170 was passed. 192 00:13:28,180 --> 00:13:30,440 Let's go ahead and knock this out over here. 193 00:13:30,490 --> 00:13:33,600 I'm gonna set up the call with the query and the updates. 194 00:13:33,670 --> 00:13:36,480 That's D.B. dot collection. 195 00:13:36,550 --> 00:13:39,550 We are updating the documents in tasks. 196 00:13:39,550 --> 00:13:44,100 I'll be using update many and I'm going to provide those two arguments. 197 00:13:44,110 --> 00:13:51,590 First up we are going to find all tasks where completed is equal to False that's complete. 198 00:13:51,610 --> 00:13:52,710 Don't forget the d.. 199 00:13:52,750 --> 00:13:54,910 Otherwise it won't work as expected. 200 00:13:54,910 --> 00:13:55,930 Perfect. 201 00:13:55,990 --> 00:13:59,080 Then we're going to provide that second object. 202 00:13:59,080 --> 00:14:05,380 This is the list of operations we want to perform now in our case we just want to set a field value 203 00:14:05,410 --> 00:14:11,050 so I will use set and we are going to set completed equal it to true. 204 00:14:11,050 --> 00:14:13,510 So find the docs where completed is false. 205 00:14:13,540 --> 00:14:16,370 Set it over to a completed value of true. 206 00:14:16,540 --> 00:14:18,970 That's step 2 step 3. 207 00:14:18,970 --> 00:14:23,570 Use the promised method to set up our handlers dot then right here. 208 00:14:23,620 --> 00:14:31,760 Here we will get access to the result and then down below we can add a call to catch where we will get 209 00:14:31,760 --> 00:14:37,130 access to the error should that promise get rejected perfect. 210 00:14:37,150 --> 00:14:39,940 Now if the promise is fulfilled what are we going to do. 211 00:14:40,000 --> 00:14:41,830 We'll just dump that result again. 212 00:14:41,830 --> 00:14:45,730 Actually we can just dump something like the modified count. 213 00:14:45,760 --> 00:14:50,070 Now you didn't need to dump the same thing to the council if you just printed result. 214 00:14:50,080 --> 00:14:53,230 That's perfectly fine right here result. 215 00:14:53,230 --> 00:14:56,080 Dot modified count. 216 00:14:56,140 --> 00:14:57,310 Perfect. 217 00:14:57,310 --> 00:14:59,130 Next up down below for the error. 218 00:14:59,140 --> 00:15:01,500 We will just print that in its entirety. 219 00:15:01,870 --> 00:15:04,320 And now we can go ahead and test things out. 220 00:15:04,450 --> 00:15:06,970 So we looked at the docs we set up the call. 221 00:15:07,150 --> 00:15:11,500 We set up the promised handler methods and now it's time to test the work. 222 00:15:11,500 --> 00:15:16,390 Now I'm going to pull up robo three ti one more time and give things are refreshed just to make sure 223 00:15:16,390 --> 00:15:18,370 the data is what I expected. 224 00:15:18,370 --> 00:15:23,530 We have two tasks with the completed value of false and from the terminal down below. 225 00:15:23,530 --> 00:15:24,940 We'll run our script. 226 00:15:25,030 --> 00:15:26,510 I'm going to run it. 227 00:15:26,710 --> 00:15:30,650 I can see that I have two documents updated which does sound right. 228 00:15:30,760 --> 00:15:34,730 And if I refresh things over in rowboat 3 T what do I see. 229 00:15:34,750 --> 00:15:37,300 I see that all of my tasks are now completed. 230 00:15:37,300 --> 00:15:39,010 Which is fantastic. 231 00:15:39,010 --> 00:15:39,910 So there we go. 232 00:15:39,910 --> 00:15:41,490 Challenge complete. 233 00:15:41,500 --> 00:15:46,290 We were able to use this new method to achieve the desired result. 234 00:15:46,300 --> 00:15:51,460 Now we can remove the challenge comments and that's where we're gonna stop for this one in the next 235 00:15:51,460 --> 00:15:52,240 video. 236 00:15:52,240 --> 00:15:55,050 We're going to move on to the final letter in crud. 237 00:15:55,330 --> 00:15:58,600 I know how to create I know how to read and update. 238 00:15:58,600 --> 00:16:03,640 It's time to learn how to delete documents when we're done with them or they're no longer needed. 239 00:16:03,640 --> 00:16:04,830 We're gonna cover that next. 240 00:16:04,840 --> 00:16:06,330 So let's jump right in.