1 00:00:02,160 --> 00:00:09,660 Now with the basic crud operations covered, let's dive a bit deeper and let's dive deeper into update. 2 00:00:09,720 --> 00:00:16,050 First of all, let's have a quick look at all our data in the collection again, 3 00:00:16,140 --> 00:00:21,090 we got these two flights and let's say I want to change the first flight, 4 00:00:21,180 --> 00:00:28,230 I want to change it by adding an extra field which simply holds the information whether the flight is delayed 5 00:00:28,260 --> 00:00:29,250 or not. 6 00:00:29,250 --> 00:00:39,790 Now we can do that by accessing flight, whoops, flight data here and then using updateOne and I can filter by 7 00:00:39,790 --> 00:00:42,930 ID, we can copy that objectId 8 00:00:42,930 --> 00:00:46,300 and it's important that you copy the whole object and 9 00:00:46,310 --> 00:00:50,380 we could filter for _id with this objectId. 10 00:00:50,410 --> 00:00:53,760 Now this will select this first flight and now you learned, 11 00:00:53,770 --> 00:00:58,600 we can pass a document with $set where we can tell what to change, 12 00:00:58,600 --> 00:01:05,470 for example we can set delayed to true. If I now hit enter, this was accepted and if I output all the 13 00:01:05,470 --> 00:01:07,970 flights again, we see the delayed was added. 14 00:01:08,350 --> 00:01:11,860 So this is something we can do and something we already saw. 15 00:01:11,860 --> 00:01:19,950 Now there also is another command we could use and that is not updateOne but just update, 16 00:01:20,120 --> 00:01:27,340 so let me quickly remove the one here and now just use update. If I do that, you see it generally worked 17 00:01:27,430 --> 00:01:29,320 but nothing was modified 18 00:01:29,320 --> 00:01:33,710 and the reason for that simply is that we already had a value of true here, 19 00:01:33,760 --> 00:01:40,330 so if I changed delayed to false, you see it did modify one element and if I now output all the data, 20 00:01:40,330 --> 00:01:42,840 we see delayed is false. Now update works 21 00:01:42,900 --> 00:01:49,280 a bit like updateMany, updateMany was used to update all matching elements 22 00:01:49,280 --> 00:01:53,010 and update would also update all matching elements. 23 00:01:53,020 --> 00:01:59,740 So what's the difference between update and updateMany then? The difference can be seen if I actually 24 00:02:00,220 --> 00:02:02,960 remove $set here, 25 00:02:02,980 --> 00:02:10,110 so if my second argument is now just delayed false in a document. Remember with updateOne, 26 00:02:10,120 --> 00:02:13,690 this gave us an error and the same by the way is true 27 00:02:13,780 --> 00:02:18,740 if you would use updateMany here, you also get an error here. 28 00:02:19,150 --> 00:02:20,690 Now with update, 29 00:02:20,710 --> 00:02:24,880 so just update without many or one, this actually works, 30 00:02:24,890 --> 00:02:28,300 you see it was accepted and one document is changed. 31 00:02:28,450 --> 00:02:33,290 Now how would you imagine that our flight looks like right now? 32 00:02:34,620 --> 00:02:36,830 Well let's access the flight data 33 00:02:37,750 --> 00:02:40,180 and find it all and output it 34 00:02:40,690 --> 00:02:48,070 and what we see is we got two documents but the first one is very small because it now only has delayed 35 00:02:48,090 --> 00:02:52,000 false and the ID. The ID still is the old ID by the way, 36 00:02:52,000 --> 00:02:59,410 this was not changed, it still is the ID, did not change that but it overwrote all the other key value 37 00:02:59,410 --> 00:03:06,700 pairs and that is the thing about update. Update does accept this syntax with just an object and it will 38 00:03:06,700 --> 00:03:13,240 then take this object and basically replace the existing object with this new object, with this new document, 39 00:03:13,240 --> 00:03:15,290 it will only keep the ID, 40 00:03:15,310 --> 00:03:22,780 it will only patch the existing object instead of replace it, like updateOne and many do it 41 00:03:22,780 --> 00:03:27,940 if you use the $set way, if you use it like this, you override it. 42 00:03:27,940 --> 00:03:31,100 Maybe this is what you want but it is something to be aware of 43 00:03:31,330 --> 00:03:36,550 and in general, I recommend using updateOne and many to avoid this issue. 44 00:03:36,790 --> 00:03:42,410 If you want to replace something, you should use a different method, on flight data, 45 00:03:42,410 --> 00:03:44,400 you should then use replaceOne 46 00:03:44,620 --> 00:03:46,200 and there you again 47 00:03:46,300 --> 00:03:47,750 place your filter, 48 00:03:48,070 --> 00:03:54,850 so looking for ID equal to our objectId and then you add the element you want to replace it with as 49 00:03:54,850 --> 00:03:56,060 a second value. 50 00:03:56,350 --> 00:04:03,080 So here I'll take my flight data from the attached json document which was attached to earlier lectures, 51 00:04:03,100 --> 00:04:06,660 copy that, delete the closing parentheses, 52 00:04:06,880 --> 00:04:10,580 add my document and now close it and if I now hit enter, 53 00:04:10,600 --> 00:04:17,290 you see one element was modified and if we now output all the elements again, you see the ID still is 54 00:04:17,290 --> 00:04:18,460 the same as before, 55 00:04:18,480 --> 00:04:24,790 it's the ID we looked for in the filter but the rest was replaced and this is simply a more explicit and 56 00:04:24,790 --> 00:04:27,540 therefore safer way of replacing the data 57 00:04:27,550 --> 00:04:29,380 if you really want to replace it 58 00:04:29,450 --> 00:04:35,510 otherwise if you just want to change it partially, change it maybe, use updateOne or updateMany. 59 00:04:35,530 --> 00:04:38,250 Be aware of the behavior of update.