1 00:00:02,350 --> 00:00:10,820 Let's now jump back to the user database and in there, just a quick reminder we got that user data. 2 00:00:10,870 --> 00:00:20,380 Now let's say we want to find all users who have a hobby of sports and the frequency should be equal 3 00:00:20,590 --> 00:00:22,100 to two, 4 00:00:22,230 --> 00:00:25,860 so the frequency of the sports hobby. 5 00:00:26,040 --> 00:00:32,500 Let's try this, let's reach out to our users and we want to find all users where this is true, 6 00:00:32,520 --> 00:00:34,560 now how could this query look like? 7 00:00:35,010 --> 00:00:41,910 Well we could use the and keyword and then we have multiple conditions and the first condition is that 8 00:00:41,910 --> 00:00:47,820 for hobbies, whoops, for hobbies.title, we're looking for sports, 9 00:00:47,840 --> 00:00:49,140 right this was the first part, 10 00:00:49,190 --> 00:00:50,840 it should be sports. 11 00:00:50,840 --> 00:00:58,000 The second condition could be that hobbies.frequency should be equal to 2 12 00:00:58,250 --> 00:01:02,110 and that also has to be wrapped in curly braces 13 00:01:03,010 --> 00:01:09,570 or let's say it should actually be greater than or equal to 2. 14 00:01:10,350 --> 00:01:11,520 If I hit enter, 15 00:01:11,730 --> 00:01:13,010 yeah find Anna, make sense, 16 00:01:13,000 --> 00:01:15,570 she has sports and frequency two 17 00:01:15,630 --> 00:01:20,370 and I find the whole document of course because find always gives me the whole document even if I'm 18 00:01:20,370 --> 00:01:23,000 just digging in some array or embedded document, 19 00:01:23,010 --> 00:01:28,280 it doesn't return the embedded document but always the full document which contains the embedded document. 20 00:01:28,410 --> 00:01:36,840 So I find Anna because she has sports title two, frequency two, I also find Max because there, I got sports frequency 21 00:01:36,840 --> 00:01:38,250 three. 22 00:01:38,320 --> 00:01:42,990 Now let's say I repeat that query and modify it slightly 23 00:01:43,420 --> 00:01:48,150 and now I'm looking for a frequency greater than or equal to three, 24 00:01:48,170 --> 00:01:50,180 now what's that? 25 00:01:50,390 --> 00:01:56,260 I still find Anna even though for her sports hobby, the frequency is two 26 00:01:56,570 --> 00:02:01,370 but our query was that the frequency should be greater than or equal to three, 27 00:02:01,610 --> 00:02:03,500 so what's going wrong here? 28 00:02:04,040 --> 00:02:11,480 The thing is with this query, we're basically saying find me all documents where in hobbies, 29 00:02:11,630 --> 00:02:19,880 there is a document at least one document with the title sports and a document with the frequency greater 30 00:02:19,880 --> 00:02:21,230 than or equal to three, 31 00:02:21,260 --> 00:02:23,610 it does not have to be the same document, 32 00:02:23,630 --> 00:02:24,790 that is our issue here 33 00:02:25,070 --> 00:02:30,860 and indeed for Anna, yoga has a frequency greater than or equal to three. 34 00:02:30,950 --> 00:02:33,000 She has a sports element 35 00:02:33,020 --> 00:02:40,010 so the first part is satisfied and the second part is satisfied with that other element. 36 00:02:40,010 --> 00:02:45,500 Now of course it's not that uncommon that you want to ensure that one and the same element should match 37 00:02:45,500 --> 00:02:46,460 your conditions 38 00:02:46,850 --> 00:02:53,150 and for that, you have the elemMatch operator. So this query 39 00:02:53,500 --> 00:03:00,430 and I'll just count it so that we have it still on page, this query can be replaced with another query 40 00:03:00,460 --> 00:03:04,900 that should do what we want. Instead of and, 41 00:03:05,110 --> 00:03:18,640 we specify our array hobbies and then a value which is a document where we use $elemMatch, 42 00:03:19,570 --> 00:03:20,010 that 43 00:03:20,020 --> 00:03:22,770 then again takes a document as a value 44 00:03:22,840 --> 00:03:26,250 and here, we then describe how to match our element. 45 00:03:26,450 --> 00:03:32,350 Now if hobbies were just an array of numbers let's say, you could immediately start typing greater than 46 00:03:32,350 --> 00:03:35,590 30 but lower than 50 for example, 47 00:03:35,590 --> 00:03:39,210 so that would be the case if hobbies would really just be an array of numbers, 48 00:03:39,220 --> 00:03:41,060 not of embedded documents. 49 00:03:41,080 --> 00:03:47,920 However here, we have embedded documents and in this case, $elemMatch is a document where you now describe 50 00:03:48,640 --> 00:03:51,520 what to look for inside of the embedded document, 51 00:03:51,520 --> 00:04:00,110 so here we're saying title should be sports and the frequency should be 52 00:04:00,350 --> 00:04:05,780 and now here we can still write our old condition gte three. 53 00:04:05,820 --> 00:04:10,860 So this is how we can now use $elemMatch on an array with embedded documents, 54 00:04:10,890 --> 00:04:18,060 we essentially you could say describe how a single document should look like to match our query and 55 00:04:18,060 --> 00:04:24,480 if it finds at least one document in the hobbies array that has a title sports and a frequency greater 56 00:04:24,480 --> 00:04:25,820 than or equal than three, 57 00:04:25,950 --> 00:04:31,880 it will include the overall document that contains that hobbies array in the result set. 58 00:04:31,920 --> 00:04:38,370 If we got multiple elements in the hobbies array fulfilling this criteria, that would also be fine. 59 00:04:38,370 --> 00:04:45,270 And if I now hit enter, I only find Max and not Anna anymore because Max is the only user who has a 60 00:04:45,270 --> 00:04:49,830 hobby with a title of sports and a frequency of three.