1 00:00:02,160 --> 00:00:08,850 Now what we did in the last lecture where we updated the matched array element can also be done to multiple 2 00:00:08,940 --> 00:00:11,020 matching elements. 3 00:00:11,040 --> 00:00:22,910 So if I again find some documents based on an array based query, so where I maybe find hobbies with a frequency 4 00:00:24,070 --> 00:00:26,660 that is greater than two, 5 00:00:27,040 --> 00:00:35,020 so now I should find way more documents if I pretty print this, I find a bunch of documents and again always 6 00:00:35,020 --> 00:00:40,020 remember, the document is the overall person that documented the collection, not the document 7 00:00:40,150 --> 00:00:46,620 you filter for in your query, that is just the key factor for giving you back to the overall document. 8 00:00:46,900 --> 00:00:52,120 So we found Maria because she has a hobby with a frequency greater than two, 9 00:00:52,120 --> 00:00:53,360 same is true for Chris, 10 00:00:53,380 --> 00:00:56,500 he also has a hobby with a lower frequency but that does not matter, 11 00:00:56,500 --> 00:00:57,980 not all has to match, 12 00:00:58,030 --> 00:00:59,950 he has at least one that matches, 13 00:00:59,950 --> 00:01:04,390 that's the thing. We also found Anna and Manuel and Max, 14 00:01:04,390 --> 00:01:12,200 so we found all documents. However not all array documents fulfilled our filter, 15 00:01:12,490 --> 00:01:13,700 we had greater than two, 16 00:01:13,780 --> 00:01:17,380 so for example cars for Manuel did not fulfill it 17 00:01:17,770 --> 00:01:19,540 and so did hiking. 18 00:01:19,540 --> 00:01:25,690 Now let's say we want to change all embedded documents in the hobbies array that did fulfill it. 19 00:01:25,720 --> 00:01:28,630 So this was our query, 20 00:01:28,810 --> 00:01:31,540 it gave us back all documents in our collection, 21 00:01:31,540 --> 00:01:35,110 now let's update many documents 22 00:01:35,110 --> 00:01:43,150 and here, I will just repeat my query, so hobbies frequency greater than 2 23 00:01:43,320 --> 00:01:45,170 and now I want to update them, whoops 24 00:01:45,630 --> 00:01:47,830 I pressed enter accidentally, 25 00:01:47,970 --> 00:01:50,040 so now I want to update them. Again 26 00:01:50,040 --> 00:01:51,840 we can use $set inc 27 00:01:51,870 --> 00:01:52,440 and so on 28 00:01:52,460 --> 00:01:57,800 but here, I want to set a new field value, so $set is the right option here 29 00:01:58,320 --> 00:02:02,120 and I want to change all these matched hobbies. 30 00:02:02,430 --> 00:02:09,450 So before, we had hobbies.$ and then we could override this as a whole value or add a new field 31 00:02:09,450 --> 00:02:13,280 or edit an existing field with another dot notation. 32 00:02:13,290 --> 00:02:20,210 However this only matches or edits the first matching hobby for each person 33 00:02:20,310 --> 00:02:25,680 and if there are multiple matching hobbies per person, it will not update them all. 34 00:02:26,070 --> 00:02:35,880 I can show this to you by simply adding another field, good frequency and setting this to true. 35 00:02:35,990 --> 00:02:39,800 If I hit enter, it modified all five documents 36 00:02:39,800 --> 00:02:49,380 but now if I find all my persons, whoops and I pretty print that, Maria has a good frequency, 37 00:02:49,390 --> 00:02:51,420 Chris has it but note that 38 00:02:51,440 --> 00:02:53,270 these two documents were not changed 39 00:02:53,330 --> 00:02:58,970 and especially for this one, it's strange because the frequency of three is certainly higher than the 40 00:02:58,970 --> 00:03:05,720 frequency of two which we looked for, only the first element was updated. Now for Anna it made sense that 41 00:03:05,720 --> 00:03:10,170 the first one was not touched because frequency of two did not meet our criteria, 42 00:03:10,280 --> 00:03:14,220 this one was updated though, for Manuel the same is the case, 43 00:03:14,210 --> 00:03:19,820 the second one shouldn't be updated because the frequency of two is not fulfilling our filter criteria, 44 00:03:20,750 --> 00:03:25,780 for Max however, again the second document just wasn't edited even though it should have been. 45 00:03:26,090 --> 00:03:33,080 Well it should have not been because dollar sign as we used it simply refers to the first match. 46 00:03:33,080 --> 00:03:36,790 Now sometimes you want to edit all elements in an array 47 00:03:36,920 --> 00:03:38,960 and for now, let's ignore a filter, 48 00:03:39,020 --> 00:03:46,140 so let's say not the ones that match some filter but you simply want to update all elements in an array. 49 00:03:47,140 --> 00:03:51,570 Now for this, you can of course again update many 50 00:03:51,920 --> 00:04:05,050 and let's say we now want to find all persons with an age older than 30, so age should be greater than 51 00:04:05,050 --> 00:04:06,080 30, 52 00:04:06,290 --> 00:04:09,420 let's quickly pretty print that, 53 00:04:09,770 --> 00:04:11,400 whoops we 54 00:04:11,420 --> 00:04:19,360 first of all find them not update them and of course it should be total age, we renamed that, 55 00:04:19,680 --> 00:04:21,980 so we find Chris and we find Manuel. 56 00:04:22,020 --> 00:04:29,250 Now let's say for these people, I want to add a new field to my documents here, to all documents in hobbies 57 00:04:29,250 --> 00:04:37,830 not just to one. To do that, I can again use update many and then repeat my filter criteria, 58 00:04:37,840 --> 00:04:45,870 so total age should be greater than 30 and then to update them, I can of course use $set, 59 00:04:45,900 --> 00:04:48,860 I could also use inc and so on, 60 00:04:48,870 --> 00:04:52,700 let's actually use inc to match it up, so I can use inc here 61 00:04:52,980 --> 00:04:59,380 and then here, I want to update my hobbies but I don't want to override the entire field, 62 00:04:59,520 --> 00:05:05,160 I just want to add a new field to every document inside of hobbies. 63 00:05:05,160 --> 00:05:11,640 Now let's try accessing frequency with hobbies.frequency and increment it by minus one 64 00:05:11,640 --> 00:05:15,340 because these people should slow down maybe or do more, 65 00:05:15,350 --> 00:05:18,050 whatever you want but let's decrement it. 66 00:05:18,410 --> 00:05:20,820 If I hit enter, I get an error, 67 00:05:20,870 --> 00:05:24,230 cannot create field frequency in an element hobbies. 68 00:05:24,290 --> 00:05:31,310 Now the problem here is that hobbies is an array and not a document itself and even though you can find 69 00:05:31,880 --> 00:05:36,320 elements with this notation, you can't update 70 00:05:36,320 --> 00:05:43,130 but you can update all array elements with another placeholder provided by mongodb and that 71 00:05:43,130 --> 00:05:45,020 placeholder is 72 00:05:45,020 --> 00:05:48,230 ${}, whoops, $[]. 73 00:05:48,280 --> 00:05:55,580 This simply means update all elements and for each element, simply the frequency field because we have 74 00:05:55,640 --> 00:05:57,200 embedded documents in our case, 75 00:05:57,200 --> 00:06:03,860 if you hadn't embedded documents, you could of course stop after this part. And now if I hit enter, 76 00:06:03,890 --> 00:06:10,070 you see two documents were modified and if I find my documents again, you see the frequency here for 77 00:06:10,070 --> 00:06:13,790 example is zero now and previously it was one, 78 00:06:13,790 --> 00:06:18,310 so all the frequencies actually decreased by one. 79 00:06:18,320 --> 00:06:20,640 So this is the 80 00:06:20,690 --> 00:06:24,560 $[] syntax for updating all array elements.