1 00:00:02,320 --> 00:00:10,150 Now that we got the basic field operators set, let's see how we can work with arrays because there are 2 00:00:10,150 --> 00:00:18,250 some nice operators that help us with updating existing arrays in documents, like the hobbies array in my 3 00:00:18,250 --> 00:00:18,730 users, 4 00:00:18,730 --> 00:00:20,740 so I switch back to that users collection 5 00:00:20,740 --> 00:00:22,220 we worked with before. 6 00:00:22,600 --> 00:00:27,000 Let's say we want to update all documents, 7 00:00:27,070 --> 00:00:35,080 so Chris, Anna, Max, whoever matches, all documents where the person has a hobby of sports with a frequency 8 00:00:35,530 --> 00:00:42,070 greater or equal to three, this also practices something we used in the read section. 9 00:00:42,070 --> 00:00:44,470 So how could this look like? 10 00:00:44,860 --> 00:00:51,340 Well we want to first of all find all the persons so that we get a feeling for the correct filter we should 11 00:00:51,340 --> 00:00:52,070 write, 12 00:00:52,450 --> 00:00:59,500 so I want to find a certain amount of persons and I want to find persons based on the hobbies array 13 00:00:59,620 --> 00:01:05,200 and then on the hobbies array, I got two conditions. Two conditions on the same field means that I need 14 00:01:05,470 --> 00:01:14,120 and and then here, the first condition is that for hobbies.title, 15 00:01:14,200 --> 00:01:27,460 I want to have sports and the second condition is that for hobbies.frequency, I want to have a value greater 16 00:01:27,460 --> 00:01:30,840 or equal to three. 17 00:01:30,850 --> 00:01:38,740 Now if I now hit enter, we get Chris, sports frequency five, makes sense, we get Anna, sports frequency 18 00:01:38,740 --> 00:01:39,280 two. 19 00:01:39,550 --> 00:01:42,470 Well we've been there, right, we've been in that situation 20 00:01:42,490 --> 00:01:46,030 in the read module, if you haven't been there, you should go there now. 21 00:01:46,270 --> 00:01:48,970 Now how did this happen? 22 00:01:49,090 --> 00:01:57,160 This simply checks for documents where you have a hobby with a title of sports and where you have a hobby 23 00:01:57,160 --> 00:01:59,750 with a frequency greater or equal to three 24 00:02:00,010 --> 00:02:01,310 and that is the case for Anna, 25 00:02:01,420 --> 00:02:07,630 she has a hobby with the title of sports and she has a hobby with a frequency of three or greater or equal 26 00:02:07,660 --> 00:02:08,750 three. 27 00:02:08,770 --> 00:02:15,040 Now of course, we're looking for one and the same document but we are not expressing this in this query. 28 00:02:15,850 --> 00:02:20,580 The correct query would be to use elemMatch, 29 00:02:20,650 --> 00:02:29,710 so there we would normally say hobbies should have a value which is a document where we use elemMatch 30 00:02:30,190 --> 00:02:37,260 and elemMatch is now looking at the elements and hobbies which happened to be embedded documents, 31 00:02:37,270 --> 00:02:46,510 so now we can say the title key should be sports and for that same embedded document, the frequency should 32 00:02:46,510 --> 00:02:50,660 be greater or equal to three. 33 00:02:50,800 --> 00:02:56,770 And if I now hit enter, we find Chris because we have sports with the frequency greater or equal to 34 00:02:56,770 --> 00:02:59,680 three and we find Max where the same is true, 35 00:02:59,890 --> 00:03:06,730 Anna is not part of the results set anymore. So this is the right query to find the documents I want 36 00:03:06,730 --> 00:03:07,680 to update. 37 00:03:08,050 --> 00:03:12,450 Now let's say this document is the document I want to update 38 00:03:12,460 --> 00:03:17,070 but then, I want to update that document in the array, 39 00:03:17,110 --> 00:03:22,870 so essentially I know which overarching document is what I want to change but then in the document, 40 00:03:22,930 --> 00:03:27,820 I want to change something in exactly that element which I found in the array. 41 00:03:27,830 --> 00:03:37,200 Now we can do that, I'll quickly count here so that this stays on the same page, we can do that with update 42 00:03:37,220 --> 00:03:45,260 many because we'll have potentially and also in reality more than one result, I can now copy that whole 43 00:03:45,260 --> 00:03:48,530 query here because that is the query I need down there 44 00:03:49,010 --> 00:03:55,980 and now how do I want to update this? By default if I now use $set and then define something here, 45 00:03:56,000 --> 00:03:59,960 these are of course changes that will be applied to the overall document, 46 00:03:59,960 --> 00:04:06,330 so to the overall person, not to the document in the array which I found. If I want to update that array 47 00:04:06,380 --> 00:04:11,690 document, I can use a different syntax, I can use set, 48 00:04:11,690 --> 00:04:16,550 that is correct but then in here, I don't want to update hobbies as a whole, 49 00:04:16,580 --> 00:04:19,530 I don't want to assign a brand new value to hobbies, 50 00:04:19,670 --> 00:04:27,230 I only want to update the element in hobbies which matched my condition here. And to do that, we can use 51 00:04:27,230 --> 00:04:28,560 a different syntax, 52 00:04:29,730 --> 00:04:36,650 I can enclose it in quotation marks and say hobbies.$ and this will automatically refer 53 00:04:36,870 --> 00:04:39,450 to the element matched in our filter, 54 00:04:39,450 --> 00:04:44,450 so in our query and then here, I can define the new value. 55 00:04:44,460 --> 00:04:52,110 So here I could now say that should still have a title of sports, that should still have a frequency of, 56 00:04:52,200 --> 00:04:52,620 well of 57 00:04:52,620 --> 00:05:00,630 what? I could do that but the frequency of course differs, it's five for Chris and three for Max and I don't 58 00:05:00,630 --> 00:05:06,660 want to override it, essentially I don't want to override the entire document here, 59 00:05:06,690 --> 00:05:13,440 instead what I want to do is I want to add let's say a new field and we can do that by moving back to 60 00:05:13,450 --> 00:05:14,300 hobbies.$ 61 00:05:14,290 --> 00:05:24,870 and dot something else, we could add a new field here, we could say dot high frequency and 62 00:05:24,930 --> 00:05:28,610 set this to true as a value. 63 00:05:28,610 --> 00:05:35,150 So what this will do is it will dig into hobbies, find the element which we found in our query and then 64 00:05:35,540 --> 00:05:41,800 access the high frequency field which if it doesn't exist simply gets added. If I now hit enter, 65 00:05:41,890 --> 00:05:47,230 you see it matched two documents, two persons and it modified two persons 66 00:05:47,390 --> 00:05:55,400 and if we now have a look at all persons again, we see that Chris still has sports and five but also 67 00:05:55,400 --> 00:05:56,860 high frequency true, 68 00:05:57,230 --> 00:06:03,710 Anna was not edited because she had a low frequency here and therefore was not selected in our query, 69 00:06:04,520 --> 00:06:06,520 Manuel doesn't have sports at all 70 00:06:06,560 --> 00:06:10,700 but Max also has sports and now also got the high frequency field. 71 00:06:10,880 --> 00:06:13,880 So this dollar sign placeholder 72 00:06:13,880 --> 00:06:16,630 here is a great helper 73 00:06:16,730 --> 00:06:23,900 when you have a query where you do select a specific element in an array and you then want to update 74 00:06:23,990 --> 00:06:28,010 exactly that element in your set operation. 75 00:06:28,220 --> 00:06:34,040 And just to make it really clear, inside of set, you could of course have updated totally other fields 76 00:06:34,040 --> 00:06:34,810 as well, 77 00:06:34,850 --> 00:06:42,920 you could update this high frequency field and then also update isSporty or the name or add an age 78 00:06:42,920 --> 00:06:44,600 to the overall document. 79 00:06:44,600 --> 00:06:50,870 $set to works as before, it's just this special placeholder which you can use in the path to 80 00:06:50,870 --> 00:06:54,400 a field to quickly get access to the matched array element.