1 00:00:01,180 --> 00:00:02,570 Welcome back. 2 00:00:02,570 --> 00:00:05,269 I hope you're having fun so far in this section. 3 00:00:05,269 --> 00:00:08,429 I know that I am and so let's now keep it moving 4 00:00:08,429 --> 00:00:12,683 and learning how to update documents with Mongodb. 5 00:00:14,720 --> 00:00:17,087 So back in our mongoshell let's now 6 00:00:17,087 --> 00:00:20,807 use db.tours.updateOne. 7 00:00:23,884 --> 00:00:25,150 Okay? 8 00:00:25,150 --> 00:00:28,050 So that's similar to .insertone that we used 9 00:00:28,050 --> 00:00:29,760 to create a new document. 10 00:00:29,760 --> 00:00:32,430 And remember how we also had .insertmany 11 00:00:32,430 --> 00:00:35,120 and so we also have .updatemany. 12 00:00:35,120 --> 00:00:37,640 Now how does .updatemany work? 13 00:00:37,640 --> 00:00:39,776 Well, first we need to select which documents 14 00:00:39,776 --> 00:00:42,992 we actually want to update and, second, we need 15 00:00:42,992 --> 00:00:46,460 to pass in the data that should be updated. 16 00:00:46,460 --> 00:00:49,277 So the first argument is basically a filter object 17 00:00:49,277 --> 00:00:52,120 just like we learned in the last lecture. 18 00:00:52,120 --> 00:00:54,286 So we basically need to query for the documents 19 00:00:54,286 --> 00:00:57,510 that we want to update and let's start with that 20 00:00:57,510 --> 00:01:00,012 starting in a very simple way. 21 00:01:00,012 --> 00:01:02,150 So I'm just going to select based 22 00:01:02,150 --> 00:01:03,150 on the name 23 00:01:05,230 --> 00:01:06,623 The Snow Adventure. 24 00:01:08,890 --> 00:01:09,723 Okay? 25 00:01:09,723 --> 00:01:11,935 So that is my very simple filter object 26 00:01:11,935 --> 00:01:15,690 so specifying the tour that I want to update 27 00:01:15,690 --> 00:01:17,500 And now the second object that I need 28 00:01:17,500 --> 00:01:21,120 to specify is what I actually want to update. 29 00:01:21,120 --> 00:01:24,930 So another object here and then we close the function. 30 00:01:24,930 --> 00:01:28,500 And so in here we need to use the set operator. 31 00:01:28,500 --> 00:01:31,455 So, again, this works using operators just like 32 00:01:31,455 --> 00:01:35,820 in these complex queries that we saw in the last video. 33 00:01:35,820 --> 00:01:39,342 So set and then we need to create yet another object 34 00:01:39,342 --> 00:01:43,280 and finally in this one we can specify the property 35 00:01:43,280 --> 00:01:47,013 that we want to update and the value we want to set it to. 36 00:01:48,140 --> 00:01:49,792 So, in this case, what we want to do 37 00:01:49,792 --> 00:01:53,403 is to set the price to 597. 38 00:01:54,600 --> 00:01:55,433 All right? 39 00:01:55,433 --> 00:01:58,710 So this one was 497 and so we're now, basically, 40 00:01:58,710 --> 00:02:01,272 increasing it to 597. 41 00:02:01,272 --> 00:02:02,430 All right? 42 00:02:02,430 --> 00:02:06,380 So let's hit return and it's then updated, our tour. 43 00:02:06,380 --> 00:02:08,651 Now if this query here would have matched 44 00:02:08,651 --> 00:02:10,778 multiple documents. 45 00:02:10,778 --> 00:02:13,034 I can't select it for some reason, 46 00:02:13,034 --> 00:02:14,590 but you can see it. 47 00:02:14,590 --> 00:02:16,656 So if this query that we did here 48 00:02:16,656 --> 00:02:19,710 would have selected multiple documents then only 49 00:02:19,710 --> 00:02:22,220 the first one would have been updated because 50 00:02:22,220 --> 00:02:24,091 we were using .updatemain. 51 00:02:24,091 --> 00:02:26,566 And so if we already know beforehand that our query 52 00:02:26,566 --> 00:02:29,020 is going to match multiple documents, 53 00:02:29,020 --> 00:02:30,900 then we should use .updatemany 54 00:02:30,900 --> 00:02:32,430 and not .updateone. 55 00:02:32,430 --> 00:02:33,263 Okay? 56 00:02:33,263 --> 00:02:35,380 Now just to show you that it actually worked, 57 00:02:35,380 --> 00:02:38,560 let's take a look at all the documents. 58 00:02:38,560 --> 00:02:43,560 So db.tours.find and, indeed, 59 00:02:44,550 --> 00:02:46,650 we have The Snow Adventure here 60 00:02:46,650 --> 00:02:48,140 with the new price. 61 00:02:48,140 --> 00:02:48,973 Great. 62 00:02:48,973 --> 00:02:52,390 So we updated a property that already existed. 63 00:02:52,390 --> 00:02:55,280 So the price was already there and we simply set it 64 00:02:55,280 --> 00:02:58,590 to a new value but we can also create new properties 65 00:02:58,590 --> 00:03:02,500 and set these to new values, of course. 66 00:03:02,500 --> 00:03:04,930 And it actually works in the same way but 67 00:03:04,930 --> 00:03:06,870 let me show it to you anyway. 68 00:03:06,870 --> 00:03:09,740 And what I want to do here is to find premium tours 69 00:03:09,740 --> 00:03:12,734 and give them a premium field set to true. 70 00:03:12,734 --> 00:03:15,740 So what are our premium tours? 71 00:03:15,740 --> 00:03:18,453 Well, let's first do a find. 72 00:03:19,290 --> 00:03:21,960 So just to specify that query. 73 00:03:21,960 --> 00:03:24,430 And actually we already did that before. 74 00:03:24,430 --> 00:03:27,880 So the tours that I want should have a price greater 75 00:03:27,880 --> 00:03:32,340 than 500 and a rating greater or equal to 4.8. 76 00:03:32,340 --> 00:03:35,366 So these are the really our premium tours, basically, 77 00:03:35,366 --> 00:03:38,040 so really the ones that are most expensive 78 00:03:38,040 --> 00:03:39,960 and most well rated. 79 00:03:39,960 --> 00:03:43,630 So to create this query its actually quite simple. 80 00:03:43,630 --> 00:03:47,090 And again because we actually already did it before. 81 00:03:47,090 --> 00:03:49,709 Now what I want you to do here is to pause the video 82 00:03:49,709 --> 00:03:51,592 and even if you remember it, 83 00:03:51,592 --> 00:03:54,150 type out this query on your own. 84 00:03:54,150 --> 00:03:56,929 And if you don't remember it, well that's even better 85 00:03:56,929 --> 00:03:58,921 then you can practice what you just learned 86 00:03:58,921 --> 00:04:00,202 in the last video. 87 00:04:00,202 --> 00:04:01,450 All right? 88 00:04:01,450 --> 00:04:04,001 So please pause the video here and try to come up 89 00:04:04,001 --> 00:04:06,410 with this query on your own. 90 00:04:06,410 --> 00:04:08,640 So again we want the price to be greater than 91 00:04:08,640 --> 00:04:11,983 500 and the rating greater or equal to 4.8. 92 00:04:18,970 --> 00:04:20,950 Okay, hope you did it. 93 00:04:20,950 --> 00:04:23,070 And this is the solution. 94 00:04:23,070 --> 00:04:28,070 So price should be greater or equal than 500 95 00:04:29,533 --> 00:04:34,533 and the rating should be greater or equal to 4.8. 96 00:04:39,990 --> 00:04:41,080 Okay? 97 00:04:41,080 --> 00:04:43,450 And here we set just greater than which 98 00:04:43,450 --> 00:04:45,200 is actually the same. 99 00:04:45,200 --> 00:04:47,020 It's gonna give us the same result. 100 00:04:47,020 --> 00:04:49,893 But never mind, let's do it exactly as I said. 101 00:04:50,830 --> 00:04:54,310 And so just to make sure let's see if we get the 102 00:04:54,310 --> 00:04:57,491 perfect result and indeed this is the tour 103 00:04:57,491 --> 00:05:01,000 that has this premium price and at the same time 104 00:05:01,000 --> 00:05:02,220 this premium rating. 105 00:05:02,220 --> 00:05:03,570 Okay? 106 00:05:03,570 --> 00:05:06,500 And in this case, we have only one result but, 107 00:05:06,500 --> 00:05:09,230 of course, we might have multiple results. 108 00:05:09,230 --> 00:05:12,310 And so let's now use .updatemany. 109 00:05:12,310 --> 00:05:15,220 So db.tours.updatemany. 110 00:05:19,443 --> 00:05:22,670 And so our filter object that we want is this one. 111 00:05:22,670 --> 00:05:24,320 So I'm just copying it from here. 112 00:05:26,220 --> 00:05:28,739 So that is the first argument and then second 113 00:05:28,739 --> 00:05:32,100 is actually what we want to update. 114 00:05:32,100 --> 00:05:33,300 Remember that? 115 00:05:33,300 --> 00:05:36,270 So again we use the set operator and then 116 00:05:36,270 --> 00:05:40,287 we say that we want premium to be true. 117 00:05:40,287 --> 00:05:41,200 Okay? 118 00:05:41,200 --> 00:05:43,892 And so this time we actually create a new field here 119 00:05:43,892 --> 00:05:45,822 and we set it to true. 120 00:05:45,822 --> 00:05:46,713 Okay? 121 00:05:46,713 --> 00:05:50,130 Give that one a save and actually we were missing 122 00:05:50,130 --> 00:05:53,787 some closing brace and so we cannot really go back 123 00:05:53,787 --> 00:05:56,353 so let me try to add it here. 124 00:05:57,580 --> 00:06:00,810 But, that doesn't really work, but anyway it doesn't 125 00:06:00,810 --> 00:06:03,684 matter we can just go back to the last command 126 00:06:03,684 --> 00:06:06,490 and then correct it. 127 00:06:06,490 --> 00:06:09,190 So this is the brace that we were missing then 128 00:06:09,190 --> 00:06:11,213 close it and here we go. 129 00:06:11,213 --> 00:06:16,213 So db.tours.find just to check it now. 130 00:06:18,530 --> 00:06:21,100 And here we have the last one which now 131 00:06:21,100 --> 00:06:23,300 has premium set to true. 132 00:06:23,300 --> 00:06:25,797 And again this was the only document that matched 133 00:06:25,797 --> 00:06:30,000 our query and so it's the only one that got this new 134 00:06:30,000 --> 00:06:31,365 premium property here, 135 00:06:31,365 --> 00:06:34,120 but if there were multiple documents matching 136 00:06:34,120 --> 00:06:37,200 that query, then all of them would have gotten 137 00:06:37,200 --> 00:06:39,100 this premium true. 138 00:06:39,100 --> 00:06:39,933 Okay? 139 00:06:39,933 --> 00:06:42,680 So that's why we used .updatemany and remember 140 00:06:42,680 --> 00:06:45,463 if we used .updateone then only the first document 141 00:06:45,463 --> 00:06:48,460 matching the query gets updated. 142 00:06:48,460 --> 00:06:49,293 Okay? 143 00:06:49,293 --> 00:06:53,110 So that's how we update documents at least partially 144 00:06:53,110 --> 00:06:57,210 so with this .updatemany or .updateone we usually only 145 00:06:57,210 --> 00:07:00,440 update parts of the document, but we can also 146 00:07:00,440 --> 00:07:03,710 completely replace the content of the document. 147 00:07:03,710 --> 00:07:05,870 And for that we use .replaceone. 148 00:07:05,870 --> 00:07:08,100 I'm not going to do that, but for the sake 149 00:07:08,100 --> 00:07:11,360 of completeness I wanted to show it to you as well. 150 00:07:11,360 --> 00:07:16,360 So db.tours.replaceone or .replacemany, okay? 151 00:07:17,570 --> 00:07:19,700 And so in here, just like before, you would 152 00:07:19,700 --> 00:07:22,740 pass the search query and then new data that you 153 00:07:22,740 --> 00:07:24,763 want to put in this document. 154 00:07:24,763 --> 00:07:27,633 All right, not going to do that here. 155 00:07:29,180 --> 00:07:31,630 But now you know that it also exists. 156 00:07:31,630 --> 00:07:34,645 And like this we have three of our correct operations 157 00:07:34,645 --> 00:07:36,610 already completed. 158 00:07:36,610 --> 00:07:40,063 So creating, reading, and updating and so 159 00:07:40,063 --> 00:07:43,000 as you can guess in the next video we will talk 160 00:07:43,000 --> 00:07:44,853 about deleting documents.