1 00:00:02,170 --> 00:00:08,980 I am connected to a running mongod instance again and I'll use a dataset which we prepared in the insert 2 00:00:09,820 --> 00:00:13,360 module, in case you skipped that, you find it attached, 3 00:00:13,360 --> 00:00:20,350 you just need to import it into a new collection and database. It is this users dataset with a couple 4 00:00:20,350 --> 00:00:22,330 of users in there. 5 00:00:22,330 --> 00:00:29,350 Now here, I got some nice data and since this is the update module, now everything is about changing that 6 00:00:29,350 --> 00:00:30,280 data 7 00:00:30,310 --> 00:00:37,390 and let's start with Chris because Chris does not look like the other entries, the hobbies there are different, 8 00:00:37,390 --> 00:00:38,180 there is no age, 9 00:00:38,200 --> 00:00:39,450 there is no phone number, 10 00:00:39,490 --> 00:00:42,960 so this is a perfect use case to update this data piece 11 00:00:43,000 --> 00:00:46,210 and of course you would not just update from inside the shell, 12 00:00:46,240 --> 00:00:51,640 it's of course typical that you build an application, let's say a blog where your users can edit a blog 13 00:00:51,650 --> 00:00:56,170 post and once they hit save, you want to reflect that save in a database of course. 14 00:00:56,560 --> 00:00:59,680 So how can we update Chris? 15 00:00:59,680 --> 00:01:05,950 Well, an update operation requires two pieces of information, we need to be able to identify the document 16 00:01:05,980 --> 00:01:11,800 that should be changed and then we need to define how it should change. For identification, 17 00:01:11,800 --> 00:01:13,890 you can use any filter 18 00:01:13,900 --> 00:01:16,660 you could also use for finding documents. 19 00:01:16,660 --> 00:01:18,080 You don't have to use the ID, 20 00:01:18,100 --> 00:01:24,280 you can find Chris by his name, by the hobbies, by a combination of name and hobbies, 21 00:01:24,280 --> 00:01:30,920 this is all possible and you can also not just update one document at a time. If we want to update Chris, 22 00:01:30,940 --> 00:01:37,000 I'll do it by ID which is a common scenario but as I just mentioned, not a must do, if I want to update 23 00:01:37,020 --> 00:01:38,000 Chris, 24 00:01:38,050 --> 00:01:40,200 I'll have a look at my persons 25 00:01:40,540 --> 00:01:45,130 and there, I can use updateOne just like with insert one and insert many, 26 00:01:45,130 --> 00:01:49,780 we have update one an update many and the names are pretty self-explanatory. 27 00:01:49,790 --> 00:01:57,220 Update one simply takes the first document that matches your filter criteria and updates that even if 28 00:01:57,220 --> 00:02:00,410 multiple documents would normally match your criteria, 29 00:02:00,610 --> 00:02:06,400 update many will take your criteria, your filter and update all documents that match it. 30 00:02:06,400 --> 00:02:08,070 So here I have update one, 31 00:02:08,170 --> 00:02:15,400 now I add my filter criteria and I say ID should be equal to that objectid and then the second argument 32 00:02:15,510 --> 00:02:22,570 in the update method is basically some information on how to update this object 33 00:02:22,570 --> 00:02:27,520 and there you can use a variety of update operators. 34 00:02:27,520 --> 00:02:33,300 Now you can find all update operators in the official docs in the reference under update operators 35 00:02:33,460 --> 00:02:37,220 and I will walk you through the most important ones in this module of course. 36 00:02:37,240 --> 00:02:38,760 Right now we're here in the field 37 00:02:38,770 --> 00:02:44,840 update operators because I want to update a field or a couple of fields actually. 38 00:02:45,100 --> 00:02:47,590 Now for that, we use $set 39 00:02:47,650 --> 00:02:54,640 and we saw that earlier in the course already. $set simply takes a document where you describe 40 00:02:54,940 --> 00:03:01,210 some fields that should be changed or added to the existing document, 41 00:03:01,210 --> 00:03:04,440 so here I could say that I want to change hobbies, 42 00:03:04,600 --> 00:03:10,590 hobbies already exists and it is an array and we have specific operators for working with arrays 43 00:03:10,780 --> 00:03:16,510 but if I just want to completely override hobbies which is what I want to do here, I can use it here 44 00:03:16,510 --> 00:03:22,980 together with set and then I simply define the new value for hobbies and this should still be an array, 45 00:03:23,020 --> 00:03:24,790 hence the square brackets, 46 00:03:24,790 --> 00:03:30,130 this will not add these elements to the existing array though, it will simply override the existing 47 00:03:30,130 --> 00:03:31,060 hobbies. 48 00:03:31,060 --> 00:03:36,130 So now I will look at a couple of documents in the array where each document has a title and then we 49 00:03:36,130 --> 00:03:43,540 have sports and then a frequency like five, let's say, Chris is really sporty and then we add another document 50 00:03:43,540 --> 00:03:47,840 and now these are really just the documents that will be inside the hobbies array. 51 00:03:48,070 --> 00:03:56,950 So there we have another hobby with a title of cooking and a frequency of three let's say and then 52 00:03:56,950 --> 00:04:02,580 we have a last document in that array where we have a title of hiking 53 00:04:02,830 --> 00:04:12,870 and then we have a frequency of one. If I now hit enter, I get an error 54 00:04:13,000 --> 00:04:17,720 because the collection is not called persons but users, 55 00:04:17,830 --> 00:04:18,810 sorry about that 56 00:04:18,840 --> 00:04:21,630 and I have a typo so I also should fix that, 57 00:04:21,940 --> 00:04:29,690 it's of course update one, so little typo in there. The arguments there, that is all correct, 58 00:04:29,690 --> 00:04:31,150 nothing needs to change there. 59 00:04:31,220 --> 00:04:35,690 If I hit enter, we now get a feedback that it found one matching document, 60 00:04:35,750 --> 00:04:42,920 so for our filter, this one, it found one matching document and it also modified one document because 61 00:04:42,920 --> 00:04:46,390 the changes which we defined here made sense. 62 00:04:46,400 --> 00:04:52,220 And with that I mean that if I would run this again, the exact same command, I actually still have a matched 63 00:04:52,250 --> 00:04:57,980 count because we still have a matching document for the ID, the ID didn't change but it didn't modify 64 00:04:57,980 --> 00:05:03,160 anything because the one change that I instructed here didn't make sense, 65 00:05:03,170 --> 00:05:09,270 so mongodb ignored it because the value I wanted to set here is the already existing value. 66 00:05:09,290 --> 00:05:17,000 Now it's also interesting to see that if I now find all users, that Chris still has his name and the ID, 67 00:05:17,510 --> 00:05:22,810 so $set does not override the existing values 68 00:05:22,820 --> 00:05:31,250 instead it simply just defines some changes that mongodb evaluates and then if they make sense, 69 00:05:31,520 --> 00:05:40,460 it changes the existing document by adding or overriding these fields. All the existing fields are left 70 00:05:40,580 --> 00:05:41,930 untouched, 71 00:05:41,930 --> 00:05:43,180 they are not removed. 72 00:05:43,250 --> 00:05:49,280 You can remove fields and I'll come to that in this module but by default, it simply just adds or edits 73 00:05:49,400 --> 00:05:51,240 the fields which you specify, 74 00:05:51,260 --> 00:05:53,210 so this is update one. 75 00:05:53,300 --> 00:05:55,600 Now of course you're not limited to update one, 76 00:05:55,610 --> 00:05:57,250 you could update many 77 00:05:57,380 --> 00:06:05,510 and for that, I will find users who have and for that, let's quickly look into users who have a hobby with 78 00:06:05,510 --> 00:06:07,130 title sports. 79 00:06:07,230 --> 00:06:14,540 So let me quickly search for hobbies.title, that is how you can search for embedded documents in an 80 00:06:14,540 --> 00:06:19,210 array and that should be equal to sports, close the 81 00:06:19,240 --> 00:06:25,010 quotation marks, output this in a pretty way and you see we have Anna, Max and Chris. 82 00:06:25,030 --> 00:06:35,370 Now let's say all persons who have that hobby should receive a new field which says isSporty 83 00:06:35,370 --> 00:06:35,940 true, 84 00:06:36,060 --> 00:06:40,470 so where I set a boolean value to true for a newly added field. 85 00:06:40,470 --> 00:06:46,770 Now since I have three documents that should be updated, I could of course do it with update one three times 86 00:06:46,800 --> 00:06:54,810 and manually search the ID here with the find method, copy the ID, execute update one or I take the simpler 87 00:06:54,810 --> 00:07:05,100 route and simply replace find with update many and the filter stays the same and the second argument 88 00:07:05,160 --> 00:07:09,890 then defines the change that should be applied to every document 89 00:07:09,930 --> 00:07:17,920 and here again, I use $set to then set a new field which didn't exist before, isSporty to 90 00:07:18,010 --> 00:07:18,870 true 91 00:07:19,320 --> 00:07:20,280 and that is all. 92 00:07:20,400 --> 00:07:21,860 If I now hit enter, 93 00:07:21,990 --> 00:07:30,060 you see it found three documents and it modified three documents and if I now quickly search all users, 94 00:07:30,510 --> 00:07:38,010 you'll see that Chris has isSporty true, Anna has isSporty true and Manuel does not have it because 95 00:07:38,010 --> 00:07:42,370 there is no sports as a hobby and Max does have isSporty 96 00:07:42,390 --> 00:07:43,010 true. 97 00:07:43,140 --> 00:07:44,900 So this is now all set 98 00:07:44,910 --> 00:07:50,120 with the help of update many and that's update one and update many and $set.