1 00:00:02,120 --> 00:00:06,870 Now we worked a lot on updating elements in arrays, 2 00:00:06,870 --> 00:00:13,770 now let's also work a bit on adding and removing elements to and from arrays. Now for this, we also 3 00:00:13,770 --> 00:00:16,100 got some helpful methods we can use. 4 00:00:16,200 --> 00:00:22,380 Now let's say we want you edit Maria so we can of course update one to mix it up, 5 00:00:24,010 --> 00:00:30,410 find Maria by name and to update her, I can of course set a value here 6 00:00:30,740 --> 00:00:32,550 and I could set hobbies to, 7 00:00:32,570 --> 00:00:38,910 well to what? I can set it to a new array but I want to keep the old hobby which was good 8 00:00:38,910 --> 00:00:41,400 food and only add a new one. 9 00:00:42,030 --> 00:00:47,790 To do that, we can use a different operator, instead of $set which we still can use on different 10 00:00:47,790 --> 00:00:48,290 fields 11 00:00:48,330 --> 00:00:56,880 but instead of that for our planned update, I can use $push to push a new element onto the array. 12 00:00:57,000 --> 00:01:03,330 I can do that by now specifying a document where I describe first of all the array to which I want to 13 00:01:03,330 --> 00:01:07,180 push, hobbies and then the element I want to push, 14 00:01:07,290 --> 00:01:14,160 so in my case, an element with the title of let's say sports because Maria is sporty and then a frequency 15 00:01:14,160 --> 00:01:16,030 of let's say two. 16 00:01:16,050 --> 00:01:23,220 So with that and that's one curly brace too much, with that if I now hit enter, you see one document was modified 17 00:01:23,940 --> 00:01:32,340 and if I simply find my users, we see Maria still has good food which she had before but now 18 00:01:32,340 --> 00:01:34,910 she also has sports, so that's push. 19 00:01:35,160 --> 00:01:37,870 Push can also be used with more than one document, 20 00:01:37,950 --> 00:01:45,330 so if I repeat that and I get rid of that one document, we can also use a different syntax here and 21 00:01:45,390 --> 00:01:52,140 assign an object where I actually use $each which then is an array of multiple documents that 22 00:01:52,140 --> 00:01:53,130 should be added 23 00:01:53,420 --> 00:02:02,670 and here I could have more hobbies. I could have a title of good wine, fitting the good food I guess, frequency 24 00:02:02,670 --> 00:02:06,770 of one because we want to drink responsibly 25 00:02:06,970 --> 00:02:15,520 and then I add another hobby with a title of hiking and a frequency of two. 26 00:02:15,910 --> 00:02:20,410 Now both these elements will be pushed onto the hobbies array, 27 00:02:21,950 --> 00:02:28,100 now before I hit enter, let me show you two things you can add. For one, you could hit enter here, 28 00:02:28,160 --> 00:02:32,520 that will work but you can now also add sort, $sort 29 00:02:32,540 --> 00:02:37,130 and technically, we're still in that same object where we had $each, 30 00:02:37,130 --> 00:02:43,880 so it's a sibling to $each, sort is separated with a comma after the array which is the value for 31 00:02:43,890 --> 00:02:53,270 $each. Sort then describes how the elements in the array here should be sorted before they are 32 00:02:53,270 --> 00:02:55,040 pushed into hobbies. 33 00:02:55,040 --> 00:03:00,170 Now this might seem redundant because you could of course say well I can simply type them in the correct 34 00:03:00,230 --> 00:03:06,380 order here but always keep in mind that might be data that is coming from your users and maybe you want 35 00:03:06,380 --> 00:03:08,950 to have a sorted order in your array, 36 00:03:08,960 --> 00:03:14,030 so if your users enter ingredients to a recipe in random order, that might be fine but you want to have 37 00:03:14,060 --> 00:03:15,460 alphabetical order, 38 00:03:15,710 --> 00:03:22,450 so at server side, you can simply push all user entered values but always specify a sort 39 00:03:22,460 --> 00:03:25,460 so that you always have a sorted array. 40 00:03:25,680 --> 00:03:32,610 So therefore here I can say I want to sort by frequency in descending order to have the highest frequency 41 00:03:32,610 --> 00:03:34,080 first. 42 00:03:34,080 --> 00:03:40,520 You can also specify a slice and say only add one element of the two here, 43 00:03:40,650 --> 00:03:43,150 well it doesn't make much sense for my custom data 44 00:03:43,230 --> 00:03:48,990 but again you could have a case where your application is sending you a bunch of data but you know hey 45 00:03:49,140 --> 00:03:55,020 I want to sort and then I only take the first three elements and the rest doesn't have to be stored. 46 00:03:55,050 --> 00:03:58,080 If you have such a use case, you can add slice here, 47 00:03:58,080 --> 00:03:59,040 I will not add it, 48 00:03:59,100 --> 00:04:01,990 I'll just go with each and sort. If I now hit enter, 49 00:04:02,000 --> 00:04:08,670 one document was edited and if I search for Maria, you indeed see we have good food, we have 50 00:04:08,670 --> 00:04:13,060 sports and then we have hiking and good wine added in this order, 51 00:04:13,290 --> 00:04:21,900 so hiking comes first even though in my command, you see good wine came first but thanks to sort, this 52 00:04:21,900 --> 00:04:24,680 was re-sorted. 53 00:04:24,740 --> 00:04:31,270 Now if I would execute this again which I can do of course, then you see it's again sorted 54 00:04:31,410 --> 00:04:33,340 and it sorts as you can see 55 00:04:33,390 --> 00:04:39,180 the overall hobbies array, not just the hobbies it adds but also the existing hobbies which is great because 56 00:04:39,240 --> 00:04:41,840 then you always have a sorted array here 57 00:04:41,910 --> 00:04:44,140 no matter how many values you add.