1 00:00:02,200 --> 00:00:06,530 Now what else can we do with update besides setting some values? 2 00:00:07,170 --> 00:00:14,610 Let's look into our users again and let's say Manuel got older, he's 30 but let's say we want to increase 3 00:00:14,620 --> 00:00:16,710 that to 31. 4 00:00:16,750 --> 00:00:19,490 Now obviously, this is pretty straightforward to us, 5 00:00:19,500 --> 00:00:22,910 we could use $set and set age to 31 6 00:00:23,280 --> 00:00:28,720 but in a typical application, you will not know the age of all your users, 7 00:00:28,920 --> 00:00:35,550 so it would be nice if you wouldn't have to find all users or the user you want to update first to get 8 00:00:35,550 --> 00:00:42,210 the current age then recalculate the new age and then issue your update request, would be nice 9 00:00:42,240 --> 00:00:48,150 if for simple and common transformations like this, mongodb would do that 10 00:00:48,150 --> 00:00:51,270 and indeed it can. Incrementing a number, 11 00:00:51,270 --> 00:00:58,500 so increasing it is such a common task that mongodb has a built-in operator for this. 12 00:00:58,500 --> 00:01:02,950 So let's quickly grab the ID or let's filter by name to mix it up, 13 00:01:03,000 --> 00:01:08,060 so let's filter for Manuel, db users update one 14 00:01:08,250 --> 00:01:11,620 and then here, I'm looking for a name equal to Manuel, 15 00:01:11,700 --> 00:01:17,880 this will find the first entry or the first document with a name of Manuel in the collection 16 00:01:18,030 --> 00:01:23,570 and then here in a second argument, I don't use $set but I use the inc, 17 00:01:23,670 --> 00:01:28,950 so $inc, by the way you can also combine this. You can have $inc 18 00:01:29,160 --> 00:01:34,810 and then here you have some information which I'll add in a second 19 00:01:34,890 --> 00:01:42,270 and then as a second element in this second argument, so in this object with the purple curly brace, you 20 00:01:42,270 --> 00:01:45,450 can of course add $set and describe some other changes, 21 00:01:45,540 --> 00:01:49,230 so you can do multiple different things in the same update. 22 00:01:49,230 --> 00:01:54,950 But here I'll just use $inc and I will say that the age should be incremented by 1, 23 00:01:54,960 --> 00:01:56,300 this is what this will do 24 00:01:56,460 --> 00:01:57,990 and we could also increment by two, 25 00:01:58,110 --> 00:02:04,410 so if I do that for example and I find all results, Manuel now has an age of 32, 26 00:02:04,410 --> 00:02:06,600 it was 30 before. 27 00:02:06,600 --> 00:02:11,040 Now you can also decrement with this method even though it's called inc, 28 00:02:11,040 --> 00:02:13,680 you can also say -1 for example 29 00:02:13,680 --> 00:02:18,270 and now it will be 31 as you can see here. 30 00:02:18,630 --> 00:02:21,660 So with inc, you can go in both directions 31 00:02:21,780 --> 00:02:26,700 and as I mentioned, you can combine it and just to show this because it's really important, 32 00:02:26,790 --> 00:02:32,250 you can absolutely say increment by 1 and then $set 33 00:02:32,430 --> 00:02:39,900 and for example set isSporty which is missing to false because Manuel doesn't have sports as a hobby. 34 00:02:40,680 --> 00:02:45,220 If you do that, then you see it also succeeded and it simply did both 35 00:02:45,240 --> 00:02:53,130 and if we check Manuel again, he's now 32 and isSporty was added with a value of false. 36 00:02:53,130 --> 00:02:59,610 Now of course you could also get fancy and you could try setting the same value you're incrementing, 37 00:02:59,610 --> 00:03:04,310 so I'm incrementing age by 1 and then I'm setting age to 30, 38 00:03:04,320 --> 00:03:06,040 now let's see what happens now. 39 00:03:06,450 --> 00:03:09,360 If I do that, then I get an error, 40 00:03:09,450 --> 00:03:16,140 I get an error that updating the path age would create a conflict at age because I simply got two operations 41 00:03:16,260 --> 00:03:18,360 working on the same field 42 00:03:18,510 --> 00:03:20,330 and this is not allowed here, 43 00:03:20,340 --> 00:03:22,360 so it will simply fail. 44 00:03:22,770 --> 00:03:25,410 So you can use inc and set 45 00:03:25,530 --> 00:03:31,570 and the other operators I'll show you together but working on the same field will not be possible.