1 00:00:02,080 --> 00:00:05,220 How can we work with array data? For that, 2 00:00:05,230 --> 00:00:10,640 let me quickly find my movies and look into the structure. 3 00:00:10,720 --> 00:00:14,680 Now we got two arrays here, one of them is the genre array, 4 00:00:14,770 --> 00:00:20,500 now let's say we want to find all movies that have a genre of drama. 5 00:00:20,500 --> 00:00:22,250 Now of course you know how that works, 6 00:00:22,300 --> 00:00:28,600 you simply go to your query and there, you check that genres is equal to drama and this will look for 7 00:00:28,660 --> 00:00:34,370 all documents that have drama in the genres array. 8 00:00:34,440 --> 00:00:42,130 Now if I only want to output that drama genre then, I can add projection, 9 00:00:42,510 --> 00:00:48,780 let me maybe do this in a cleaned up console, so I can add projection, so a second argument here and 10 00:00:48,780 --> 00:00:55,020 here, I can use a special syntax and set genres.$ to one. 11 00:00:55,020 --> 00:01:01,050 Now what this means is give me the one genre you found and therefore in my genres, I only have drama 12 00:01:01,050 --> 00:01:01,520 now, 13 00:01:01,620 --> 00:01:05,330 now these items will have more genres than just drama, 14 00:01:05,490 --> 00:01:07,530 I only just fetch the drama, 15 00:01:07,530 --> 00:01:11,190 I only just output that because that might be the only thing I'm interested 16 00:01:11,220 --> 00:01:16,180 but the items behind the scenes will have more data, just as they have other fields too. 17 00:01:16,230 --> 00:01:22,030 They have more genres too but with this syntax, I only find the first match for my query here, 18 00:01:22,140 --> 00:01:27,910 if I had a more complex query for different genres, I would still just find the first match, 19 00:01:27,930 --> 00:01:37,850 let me show this to you. If I have a slightly modified query and I'm looking for genres that have all 20 00:01:38,810 --> 00:01:39,610 drama 21 00:01:43,220 --> 00:01:50,970 and horror, I now only get back horror here. Now now this might look strange at first, 22 00:01:51,000 --> 00:01:56,460 the reason for this is that all works such that it goes through the arrays and checks for the existence 23 00:01:56,460 --> 00:01:57,820 of drama and horror, 24 00:01:57,930 --> 00:02:05,190 so the first element to me both is well found when horror is confirmed to be in there too and that 25 00:02:05,190 --> 00:02:09,180 is why we output horror because that technically is the first matching element, 26 00:02:09,270 --> 00:02:12,070 drama alone didn't match anything, 27 00:02:12,090 --> 00:02:15,760 the main thing here is that it doesn't return both but only one. 28 00:02:16,140 --> 00:02:22,260 Now sometimes you could also have the case where you want to pull out some items from an array in your 29 00:02:22,260 --> 00:02:25,940 document that are not the items you queried for. 30 00:02:26,010 --> 00:02:28,200 So here's an example, for movies 31 00:02:28,290 --> 00:02:34,270 let's find all movies where genres does have a drama 32 00:02:36,120 --> 00:02:41,570 and of course if I execute it like this, we already know that result, we get the movies with the drama. 33 00:02:41,660 --> 00:02:49,630 Now what I can do is I can add some projection here, so I can add a second argument to project my values 34 00:02:49,900 --> 00:02:56,830 and say genres, well we know that I can use genres.$ and set this to one to include only 35 00:02:56,830 --> 00:03:05,560 that and now only see the result of my query but what if I actually want to now see only the horror? 36 00:03:07,330 --> 00:03:14,620 Well then here in the projection, I can set genres equal to and now I can use elemMatch here 37 00:03:14,650 --> 00:03:20,140 and we saw that before but now it works in the context of projection, 38 00:03:20,140 --> 00:03:22,420 it still works in the same way as shown before, 39 00:03:22,570 --> 00:03:26,730 so this will find us the first element for which a certain condition is met 40 00:03:28,490 --> 00:03:36,780 and here, my condition simply is equality to horror. If I now hit enter, 41 00:03:37,000 --> 00:03:40,160 you see for some documents I have no genres. 42 00:03:40,200 --> 00:03:44,160 So these are documents which will have a drama because that is what I filter for 43 00:03:44,160 --> 00:03:50,460 but then I try to pull out only the horror part and that is simply not included and for others I 44 00:03:50,460 --> 00:03:51,280 do have horror, 45 00:03:51,300 --> 00:03:55,290 so these will be items that do have both drama and horror. 46 00:03:55,290 --> 00:03:56,560 So I hope this makes sense, 47 00:03:56,590 --> 00:04:04,410 this basically gives you more flexibility to control which items of an array are displayed in your result set 48 00:04:04,830 --> 00:04:08,890 and which items of an array are important for your filtering 49 00:04:09,030 --> 00:04:11,350 and now, these don't have to match. 50 00:04:11,370 --> 00:04:17,550 So if you get cases where you want to filter for something in an array or for anything else on your 51 00:04:17,550 --> 00:04:24,180 document, it doesnt have to be filtering on an array and you then want to pull out certain values of an 52 00:04:24,180 --> 00:04:26,630 array based on some other condition, 53 00:04:26,700 --> 00:04:32,410 you can add that condition with elemMatch here on the array you're working on, 54 00:04:32,640 --> 00:04:33,850 so I hope this makes sense. 55 00:04:33,870 --> 00:04:36,960 This filter here could totally go against something else, 56 00:04:37,080 --> 00:04:39,680 we could be filtering for the rating here. 57 00:04:39,870 --> 00:04:49,830 So here, I could absolutely filter for rating.average being equal or being let's say greater than 58 00:04:49,930 --> 00:04:54,150 nine and then output that with projection 59 00:04:54,190 --> 00:05:00,520 and here we see only one element, only in one movie or series with a rating greater than nine, seems 60 00:05:00,520 --> 00:05:04,710 to have a genre of horror because it's empty for the others but now filter 61 00:05:04,780 --> 00:05:09,900 and this projection are totally unrelated and that is important to understand here.