1 00:00:02,350 --> 00:00:08,790 Now you are also not limited to querying top level fields, if I quickly find one element so that we see 2 00:00:08,790 --> 00:00:11,580 the structure again. You see in this document, 3 00:00:11,620 --> 00:00:17,130 we have a lot of embedded documents, externals, image, the network where you could see this, 4 00:00:17,260 --> 00:00:21,050 the rating which holds one nested field, schedule, 5 00:00:21,070 --> 00:00:23,800 so we have a bunch of embedded fields here, 6 00:00:23,830 --> 00:00:27,030 we also have arrays and I'll come back to querying arrays too. 7 00:00:27,100 --> 00:00:33,980 Now you can query such embedded fields as well and querying them is quite simple, 8 00:00:34,000 --> 00:00:36,690 I show you the syntax earlier already, 9 00:00:36,820 --> 00:00:41,560 you can run your find or find one query by passing a document 10 00:00:41,560 --> 00:00:51,820 and now let's say we want to look for all series or all items here where the rating is greater than 11 00:00:51,880 --> 00:00:52,710 7, 12 00:00:53,050 --> 00:00:57,220 so for example, Glee should not be included. 13 00:00:57,220 --> 00:01:02,710 Now we can do this by specifying the path to the field we're interested in, 14 00:01:02,710 --> 00:01:10,050 so in our case that's the average yield instead of the rating embedded document. You specify such a path 15 00:01:10,090 --> 00:01:14,800 by using double quotation marks around the path, this is required because otherwise the dot is considered 16 00:01:14,890 --> 00:01:20,830 an invalid character and then you have your field that holds the embedded document, 17 00:01:21,010 --> 00:01:27,150 so in my case that field name is rating, not ratings, rating, that holds the embedded document. 18 00:01:27,550 --> 00:01:32,830 So rating in my case, dot and then the name of the field 19 00:01:32,890 --> 00:01:37,900 and if you had multiple levels, let's say there you had the total rating and then you had the average, 20 00:01:38,070 --> 00:01:39,110 this would work too 21 00:01:39,310 --> 00:01:43,360 but I just happened to have an average field in there and that holds the value 22 00:01:43,360 --> 00:01:44,310 I want to query. 23 00:01:44,470 --> 00:01:46,320 Now we could query for equality 24 00:01:46,540 --> 00:01:52,240 but as I mentioned, I want to find all ratings greater than 7 and that can be done like this, 25 00:01:52,240 --> 00:01:57,130 it also doesn't matter if you use 7 or a 7.0 here, you can use both. 26 00:01:57,370 --> 00:02:01,420 Now if I do that and I have pretty print my results, 27 00:02:01,420 --> 00:02:08,290 I get back quite a lot of results but you should not find any item where the rating is below 7, that 28 00:02:08,290 --> 00:02:10,040 should not be the case. 29 00:02:10,060 --> 00:02:14,340 So there, you find an easy way of querying embedded documents 30 00:02:14,410 --> 00:02:19,960 and as I mentioned, that embedding, that path can be as deep as you need it to be, 31 00:02:19,960 --> 00:02:21,780 so you're not limited to one level, 32 00:02:21,820 --> 00:02:28,060 you can add as many dots in your path as required to dig into that embedded document. And then you can 33 00:02:28,060 --> 00:02:32,820 use all the queries selectors, all the operators on that embedded field. 34 00:02:32,860 --> 00:02:34,780 You can also query arrays, 35 00:02:34,860 --> 00:02:40,410 we got the genres array in our data which has some keywords for the different genres, 36 00:02:40,570 --> 00:02:47,090 now let's say we want to find all items which have the drama genre. 37 00:02:47,280 --> 00:02:56,170 Now to make this work, we'll use a find query again but the term here or the query expression of course 38 00:02:56,170 --> 00:02:57,340 differs. 39 00:02:57,340 --> 00:03:04,370 Now keep in mind, we're looking for the genres field, that is not an embedded document but an array, 40 00:03:04,500 --> 00:03:07,710 the approach I show you would work equally for arrays 41 00:03:07,750 --> 00:03:12,790 in embedded documents, of course you just would need to use the dot notation to reach that array in there 42 00:03:12,820 --> 00:03:14,890 but then it's just a normal field. 43 00:03:15,280 --> 00:03:18,840 So here it's a top level field named genres, 44 00:03:19,000 --> 00:03:21,650 so let's look for genres here 45 00:03:22,630 --> 00:03:26,780 and then let's try this, let's try looking for drama. 46 00:03:26,830 --> 00:03:32,020 The casing here is important by the way, drama like this will not give you anything because there is 47 00:03:32,020 --> 00:03:35,440 no drama, it always starts with a capital D, 48 00:03:35,440 --> 00:03:41,130 if I look for it like this, I do get back results and all these results will have genres 49 00:03:41,150 --> 00:03:44,420 that also includes drama and that's the important thing, 50 00:03:44,420 --> 00:03:50,680 equality here with an array does not mean that drama is the only item in the array, so it's not like 51 00:03:50,680 --> 00:03:53,410 genres is equal to just drama. 52 00:03:53,440 --> 00:03:54,970 It's just equal to 53 00:03:55,090 --> 00:03:58,910 there is drama in the array so this item does exist, 54 00:03:58,930 --> 00:04:05,160 genres is equal to drama in that drama exists in the array but there may be more genres. 55 00:04:05,470 --> 00:04:09,670 If you want exact equality, you can get that too of course, 56 00:04:09,670 --> 00:04:15,870 if you want exact equality, you can modify your query to not look for genres drama like this 57 00:04:16,000 --> 00:04:22,510 but now you include square brackets around that. That signals mongodb that you will look for genres 58 00:04:22,720 --> 00:04:25,930 being equal to exactly that array. 59 00:04:25,990 --> 00:04:29,580 If I now hit enter, I also find some documents and here 60 00:04:29,620 --> 00:04:31,630 indeed, genres is just drama, 61 00:04:31,630 --> 00:04:33,510 just an array with drama in there, 62 00:04:33,520 --> 00:04:35,930 no other genres. 63 00:04:36,370 --> 00:04:42,580 So this is really important to understand, this is how you can query arrays in its basic form either for 64 00:04:42,580 --> 00:04:46,610 exact equality or for doesn't include this item, 65 00:04:46,630 --> 00:04:49,900 both can be helpful depending on what you're trying to achieve.