1 00:00:02,190 --> 00:00:10,560 The obvious question that remains is how can I now find a certain set of persons, a certain set of documents 2 00:00:10,560 --> 00:00:13,260 and hobbies and only update those? 3 00:00:13,320 --> 00:00:20,680 Let's say we want to find all hobbies with a frequency greater than two, 4 00:00:20,680 --> 00:00:25,670 so first of all we want to find all documents where the persons have hobbies with such a frequency 5 00:00:25,780 --> 00:00:29,830 but thereafter, we also want to change the elements in the array. 6 00:00:29,830 --> 00:00:35,530 Now let's first of all find the right documents again with the help of find, here 7 00:00:35,620 --> 00:00:38,460 we dig into hobbies and frequency, 8 00:00:38,470 --> 00:00:46,150 here we can use that dot notation, in the filter it's allowed and we look for a frequency greater than 9 00:00:46,150 --> 00:00:46,520 2, 10 00:00:46,540 --> 00:00:49,170 let's say. Let's pretty print 11 00:00:49,210 --> 00:00:52,000 that and we see Maria has one hobby, 12 00:00:52,200 --> 00:00:56,580 here we also get a hobby with a frequency lower than two and also one equal to two 13 00:00:56,610 --> 00:01:00,300 which also does not make it into the greater than condition 14 00:01:00,510 --> 00:01:06,540 but here is the one fulfilling our condition and you have to remember that one fulfilling document here 15 00:01:06,600 --> 00:01:12,160 gives us the entire surrounding document because we're still looking for the surrounding document. Here 16 00:01:12,180 --> 00:01:13,200 this is the winner 17 00:01:13,320 --> 00:01:14,730 and here, this is the winner 18 00:01:14,760 --> 00:01:17,810 and here both documents are greater than 2, 19 00:01:17,870 --> 00:01:19,950 the frequency is greater than 2. 20 00:01:20,460 --> 00:01:25,150 So with that, I now want to update all hobbies where the frequency is greater than 2, 21 00:01:25,230 --> 00:01:26,820 so this one should be updated, 22 00:01:26,820 --> 00:01:28,460 this should be updated, 23 00:01:28,470 --> 00:01:29,730 this should be updated, 24 00:01:29,730 --> 00:01:32,610 this should not be updated for example. 25 00:01:32,610 --> 00:01:34,250 Now can we achieve that? 26 00:01:34,650 --> 00:01:43,740 Well we do access are users and update many and now we first of all have to find the right persons, 27 00:01:43,780 --> 00:01:46,910 so again hobbies.frequency 28 00:01:47,290 --> 00:01:53,370 and here we look for a frequency greater than two, the same we had before. 29 00:01:53,560 --> 00:02:00,890 Now in the update mechanism, I want to set something because I want to add a new field 30 00:02:01,210 --> 00:02:11,600 and now there, I can access hobbies.$ would give me the first matching element, 31 00:02:11,610 --> 00:02:14,030 this gives me all elements, 32 00:02:14,040 --> 00:02:18,940 now what I can do here is I can define an identifier here and the name is up to you, 33 00:02:18,940 --> 00:02:20,200 like el 34 00:02:20,490 --> 00:02:23,100 and then define what you want to set as a value. 35 00:02:23,130 --> 00:02:28,040 Now I will first of all add a nested document because this will refer to one document in the hobbies 36 00:02:28,040 --> 00:02:28,620 array, 37 00:02:28,680 --> 00:02:34,370 so in there I'll add a good frequency field and I want to set that to true. 38 00:02:34,650 --> 00:02:35,400 Don't hit enter, 39 00:02:35,430 --> 00:02:42,240 this will not work right now because this part will not be understood by mongodb, this el thing is 40 00:02:42,240 --> 00:02:50,190 something we defined because now we add a third argument to the update many method which also is a document. 41 00:02:50,310 --> 00:02:56,310 This is the document where you could set upsert to true, where you could also add a write concern 42 00:02:56,520 --> 00:03:01,350 but I will set an array filters key here. 43 00:03:01,500 --> 00:03:05,710 Now array filters works together with this syntax, here 44 00:03:05,880 --> 00:03:12,090 you can define some conditions by which you want to filter elements and these conditions can even differ 45 00:03:12,150 --> 00:03:13,470 from these conditions here, 46 00:03:13,560 --> 00:03:15,070 so they don't have to be equal. 47 00:03:15,150 --> 00:03:17,290 You could be looking for something totally different 48 00:03:17,290 --> 00:03:25,290 here like age greater than 30 and then still filter out certain array elements in this array and you could even 49 00:03:25,290 --> 00:03:32,250 do that for multiple arrays because with the el here, you set an identifier for the condition you want 50 00:03:32,250 --> 00:03:37,650 to apply for this update expression here. Now in array filters, 51 00:03:37,670 --> 00:03:39,200 you therefore have an array 52 00:03:39,470 --> 00:03:43,970 of the different filters for the different arrays you might be updating. 53 00:03:44,320 --> 00:03:50,930 There you have multiple documents, one for each filter and then you have to repeat your identifier, 54 00:03:50,930 --> 00:03:55,370 so in my case it's el, if you named this differently which you can, 55 00:03:55,370 --> 00:03:57,480 you have to name it differently here too. 56 00:03:57,560 --> 00:04:04,080 Now for us here, el is one element in hobbies which happens to be an embedded document, 57 00:04:04,310 --> 00:04:13,160 so if I want to include or check some fields, some field in that embedded document, I can use el dot and 58 00:04:13,160 --> 00:04:20,100 then frequency here as an example and then check whether frequency is greater than 2. 59 00:04:20,150 --> 00:04:22,810 With that I'm rebuilding this query up here 60 00:04:22,820 --> 00:04:24,160 but you don't have to do that, 61 00:04:24,170 --> 00:04:25,910 you can have totally different queries here. 62 00:04:25,910 --> 00:04:30,930 You could be looking for the age up here and still look for elements with a frequency greater than two 63 00:04:30,970 --> 00:04:31,770 here. 64 00:04:31,790 --> 00:04:35,860 Now here I am missing a square bracket to close that 65 00:04:36,080 --> 00:04:39,040 and on the other hand, I don't need these. 66 00:04:39,080 --> 00:04:45,760 So now if I hit enter, you see it modified five documents, all five documents that met this condition 67 00:04:45,770 --> 00:04:53,000 and if I now look into my users, you see Maria has a good frequency here, Chris does only have it here 68 00:04:53,030 --> 00:04:57,680 because these two indeed did not meet our condition of a frequency greater than two, 69 00:04:57,870 --> 00:05:01,190 here it was updated but not for this document, 70 00:05:01,190 --> 00:05:06,380 here it was updated for this but not for this document and for Max and that's interesting, it was updated 71 00:05:06,380 --> 00:05:09,520 for both because both met this criteria. 72 00:05:09,770 --> 00:05:16,230 So this is a nice syntax which allows you to be really flexible about filtering out elements in an array 73 00:05:16,250 --> 00:05:17,370 that should change 74 00:05:17,420 --> 00:05:24,080 and as I mentioned, here we had the same filter and then array filter, you could have totally different 75 00:05:24,080 --> 00:05:24,620 filters. 76 00:05:24,650 --> 00:05:30,680 So this filter by which you identify documents and the filter by which you identify array elements you 77 00:05:30,680 --> 00:05:31,550 want to update 78 00:05:31,730 --> 00:05:33,050 do not have to be equal.