1 00:00:02,080 --> 00:00:05,670 So in the last lecture, we had a look at or, let's now have a look at and. 2 00:00:05,810 --> 00:00:14,930 For that again, let's add a find query here and let's pass a document where we describe our filter 3 00:00:15,440 --> 00:00:16,790 and now let's use $and, 4 00:00:16,830 --> 00:00:21,220 just as or, this takes an array of documents. 5 00:00:21,530 --> 00:00:26,030 And now let's say we want to find all documents where we have a rating 6 00:00:26,330 --> 00:00:33,020 and we found that on rating.average remember, a rating that is greater than 9, so $gt 7 00:00:33,020 --> 00:00:37,370 9, so high quality item, high quality series or movie 8 00:00:37,920 --> 00:00:39,620 and and that's the second element 9 00:00:39,620 --> 00:00:40,410 in this array 10 00:00:40,490 --> 00:00:43,800 and just as with or, you can have as many as you want, 11 00:00:43,820 --> 00:00:51,430 you're not limited to two and let's say we're looking for documents that have a genre of drama. 12 00:00:51,470 --> 00:00:59,150 Again I'm querying that index with this syntax to find all items that must have drama but may have additional 13 00:00:59,150 --> 00:01:07,770 genres, so we find high quality dramas here with and. If I hit enter, we got some results and we see the rating 14 00:01:07,770 --> 00:01:13,970 is quite good here and we get drama in the genre and we got another result with a very high rating 15 00:01:14,540 --> 00:01:17,430 and drama in the genre, 16 00:01:17,480 --> 00:01:20,320 so this is working. Now 17 00:01:20,330 --> 00:01:23,780 actually, there is a shorter way of writing this. 18 00:01:23,900 --> 00:01:26,940 We can totally get rid of this, 19 00:01:26,990 --> 00:01:34,040 so this is the old syntax and I'll just count that, we find three results to leave it here on the page 20 00:01:34,130 --> 00:01:36,560 whilst I write the alternative. 21 00:01:36,560 --> 00:01:46,430 The alternative is that we don't use and but we just pass a document, one document here and in there, we 22 00:01:46,430 --> 00:01:48,650 just start writing our conditions, 23 00:01:48,650 --> 00:01:55,250 so rating.average should be and then that's exactly the same condition I have above, 24 00:01:55,390 --> 00:02:03,010 so greater than nine and then just a comma inside of our filter document to add another field to that 25 00:02:03,010 --> 00:02:11,340 filter document, genres drama, if I count that, you'll see I get back three items as well. 26 00:02:11,620 --> 00:02:19,690 So these two ways of getting the data are exactly equal because by default, mongodb ends all key 27 00:02:19,690 --> 00:02:20,230 fields 28 00:02:20,320 --> 00:02:28,660 you add to that filter document and is the default concatenation mechanism of mongodb. That of course 29 00:02:28,660 --> 00:02:36,300 leaves one question, why is there a $and operator if this does exactly the same? 30 00:02:36,580 --> 00:02:43,740 There is a $and operator because you might have multiple conditions on the same field, 31 00:02:43,810 --> 00:02:54,020 let's say on genres, if I want to find the count of items and I'm just using count to not put all the 32 00:02:54,020 --> 00:02:57,810 data on the screen immediately, on genres, 33 00:02:57,920 --> 00:03:03,660 where the genre is drama but where the genre also is horror, 34 00:03:03,860 --> 00:03:11,810 this does work here but this will actually not work in all drivers because in javascript, having repeat 35 00:03:11,900 --> 00:03:15,870 names in the same object is not permitted. 36 00:03:16,250 --> 00:03:24,420 It is for cases like this that you then can use and, so even though it works here, does not have to work 37 00:03:24,420 --> 00:03:25,860 in all drivers, 38 00:03:25,860 --> 00:03:32,810 so then you can simply wrap these queries into separate documents which you passed to that and array 39 00:03:34,370 --> 00:03:37,600 and now you will have an approach that will work fine. 40 00:03:37,880 --> 00:03:42,750 Now you will also notice that the number of results returned is different 41 00:03:42,890 --> 00:03:44,570 and the reason for that is simple, 42 00:03:44,780 --> 00:03:49,670 let me repeat that approach where I mentioned it will not work everywhere and let's look into that data. 43 00:03:50,170 --> 00:03:50,720 Now 44 00:03:50,780 --> 00:04:01,080 in these 23 results, we got drama and horror, drama, crime and horror, drama, horror 45 00:04:01,070 --> 00:04:04,900 again, drama, fantasy and horror 46 00:04:06,330 --> 00:04:07,380 but then if you scroll up, 47 00:04:07,380 --> 00:04:10,070 also an entry with just horror, 48 00:04:10,080 --> 00:04:17,530 now why is that? Because and that is what I meant, even though we don't get an error here, 49 00:04:17,850 --> 00:04:23,970 it essentially, the second genres of horror will essentially just replace that key because in json 50 00:04:23,970 --> 00:04:27,040 documents, you can't have the same key more than once 51 00:04:27,090 --> 00:04:32,480 and if you do specify it more than once, the second one will just override the first one. 52 00:04:32,520 --> 00:04:43,270 So actually the query like this which gives us back 23 results is equivalent to this query where 53 00:04:43,320 --> 00:04:45,690 we get back 23 results, 54 00:04:45,720 --> 00:04:52,980 so if we want to look for both genres, we have to use the and operator and this is why there is an and 55 00:04:52,980 --> 00:04:55,740 operator even though queries are 56 00:04:55,780 --> 00:05:01,800 anded by default. If you look for values on the same field, you need $and.