1 00:00:02,190 --> 00:00:08,250 Let's now dive into querying arrays a bit deeper because there are multiple things we can do when it 2 00:00:08,250 --> 00:00:09,840 comes to querying arrays 3 00:00:10,050 --> 00:00:14,890 and there are special operators that help us with querying arrays too. 4 00:00:14,910 --> 00:00:18,420 Now first of all, I'm back in the same database I was before 5 00:00:18,510 --> 00:00:22,730 and please note that I will also work with the data we use in the assignment, 6 00:00:22,730 --> 00:00:29,910 so if you skipped the assignment, at least download the attached data and import it as described in the 7 00:00:29,910 --> 00:00:34,980 first or as requested in the first assignment task, you don't need to complete the rest but you need 8 00:00:34,980 --> 00:00:38,010 to have that data in your database. 9 00:00:38,040 --> 00:00:46,630 So first of all, I will however switch to my user database here and in there, 10 00:00:46,640 --> 00:00:48,440 remember we had a couple of users, 11 00:00:48,500 --> 00:00:50,620 let me pretty print them, 12 00:00:50,660 --> 00:00:52,350 we had three users here. 13 00:00:52,430 --> 00:00:54,610 Now the users had hobbies, 14 00:00:54,610 --> 00:01:00,020 now let's say we want to find all users who have a hobby of sports. 15 00:01:00,020 --> 00:01:05,660 Now one problem we have is of course with that structure, where hobbies are embedded documents and not 16 00:01:05,720 --> 00:01:19,830 just strings, that it won't work if I search and pretty print for hobbies being equal to sports because 17 00:01:19,920 --> 00:01:22,180 there will be sports in the hobbies 18 00:01:22,200 --> 00:01:26,390 but that's just the value of a field of an embedded document in the array. 19 00:01:26,640 --> 00:01:29,880 So if I hit enter, I don't find any documents, 20 00:01:30,150 --> 00:01:34,800 how can I now find all hobbies that are sports? 21 00:01:34,800 --> 00:01:42,720 Well of course we could search for exact equality and pass the document here and search hobbies for nested 22 00:01:42,900 --> 00:01:50,010 documents, embedded documents where the title is sports but if I hit enter there, I also find nothing because 23 00:01:50,010 --> 00:01:54,000 we have no hobby documents in the hobbies array that look like this, 24 00:01:54,000 --> 00:01:56,260 our documents also have a frequency. 25 00:01:56,700 --> 00:02:02,840 So yes I could of course add the frequency here and I could look for a frequency of two 26 00:02:03,040 --> 00:02:10,690 and now I would find Anna but I don't find Max because for Max, the frequency is three. So this does 27 00:02:10,690 --> 00:02:12,890 not really get us anywhere, 28 00:02:12,940 --> 00:02:16,710 we can't really use this way of searching for sports 29 00:02:16,750 --> 00:02:22,630 but what if I really just want to find all documents that have an embedded sports document no matter 30 00:02:22,630 --> 00:02:24,380 what the frequency is? 31 00:02:24,580 --> 00:02:31,030 Well this is all pretty simple to do in mongodb, we just need to change our query a little bit. Instead 32 00:02:31,030 --> 00:02:36,850 of just looking for hobbies and then looking for an equal document, 33 00:02:36,850 --> 00:02:43,390 we can use a path here, wrapping it in double quotation marks with dot notation and I can look for 34 00:02:43,390 --> 00:02:46,620 hobbies.title being equal to sports. 35 00:02:46,630 --> 00:02:53,680 Now the interesting thing here is title is of course something we can see on the documents that are 36 00:02:53,770 --> 00:03:02,050 inside the hobbies array, so we can act as if hobbes would hold just an embedded document and dig into 37 00:03:02,050 --> 00:03:04,450 properties of that embedded document 38 00:03:04,510 --> 00:03:11,410 even though here we got multiple embedded documents in an array but mongodb understands this syntax 39 00:03:11,810 --> 00:03:17,780 and what it will do for this query is it will essentially go through all the elements in hobbies and 40 00:03:17,780 --> 00:03:26,470 for each element, it will dig into the document and compare title to our query value, so to sports. And 41 00:03:26,480 --> 00:03:27,070 therefore if I 42 00:03:27,080 --> 00:03:28,180 now hit enter, 43 00:03:28,370 --> 00:03:36,860 I do find Anna and Max. And this is a super useful syntax for querying array data that you absolutely 44 00:03:36,860 --> 00:03:44,810 should keep in mind, that you can use this path embedded approach, not only on directly embedded documents, 45 00:03:44,810 --> 00:03:50,930 so if you have one embedded document in a field but also, if you have an array of embedded documents 46 00:03:51,620 --> 00:03:58,640 and then you can use the same query world with greater than and everything you want as before, you don't 47 00:03:58,640 --> 00:04:00,260 just have to look for equality, 48 00:04:00,290 --> 00:04:05,310 you could have a document with greater than, lower than, with an expression, whatever you needed. 49 00:04:05,360 --> 00:04:07,360 So that is possible here too.