1 00:00:02,300 --> 00:00:10,170 With comparison operators covered, let's have a look at logical operators, and, not, nor and or. 2 00:00:10,470 --> 00:00:17,450 Now I will say it right away, the operator you'll probably use most often here is $or and I'll 3 00:00:17,450 --> 00:00:18,660 explain why too. 4 00:00:18,800 --> 00:00:24,050 So let's dive into our database or into our data sets here again, into our collection 5 00:00:24,170 --> 00:00:26,100 and let's work with these operators. 6 00:00:26,360 --> 00:00:31,250 So for that, let's quickly have a look at our structure of one document and let's say we want to 7 00:00:31,250 --> 00:00:43,060 find all documents where or all items here where the rating is either lower than three or greater than 9, 8 00:00:43,100 --> 00:00:48,520 so the very bad or the very high quality items. 9 00:00:48,740 --> 00:00:59,470 Now I'll run each query standalone to begin with, so db.movies find pretty and then let's add the filter, 10 00:00:59,500 --> 00:01:06,940 now I'm looking for rating.average and then here let's first of all check if there are any items 11 00:01:06,940 --> 00:01:08,530 with a rating lower than 3 12 00:01:08,560 --> 00:01:09,540 and there are not, 13 00:01:09,610 --> 00:01:12,460 so let's modify this slightly, 4 nope, 14 00:01:15,240 --> 00:01:15,620 5 15 00:01:15,640 --> 00:01:15,970 ok. 16 00:01:15,970 --> 00:01:17,400 So we get items, yeah 17 00:01:17,650 --> 00:01:18,350 more than one 18 00:01:18,520 --> 00:01:23,480 we're rating lower than five, two items to be precise. 19 00:01:23,550 --> 00:01:28,660 You can by the way find out how many items there are by replacing pretty with count 20 00:01:28,710 --> 00:01:32,190 and you'll just get back the number of items but more on that later. 21 00:01:32,490 --> 00:01:34,530 So we have two items, 22 00:01:34,590 --> 00:01:42,470 now let's check how many items or if we got any items with a value, a rating greater than nine. 23 00:01:42,480 --> 00:01:43,070 Let me, 24 00:01:43,110 --> 00:01:44,280 we got some items, 25 00:01:44,280 --> 00:01:47,100 let me quickly check the amount, seven, 26 00:01:47,280 --> 00:01:49,530 so let's maybe narrow this down even more, 27 00:01:49,560 --> 00:01:52,700 9.5 too much, 9.4, 28 00:01:55,410 --> 00:01:56,430 9.3, 29 00:01:56,500 --> 00:01:58,000 ok two items. 30 00:01:58,230 --> 00:02:03,950 So now let's combine these two, lower than, there we had five 31 00:02:04,170 --> 00:02:06,710 and greater than 9.3. 32 00:02:06,870 --> 00:02:09,810 Now for that, we can use the OR operator, 33 00:02:09,810 --> 00:02:18,640 so I can write a new query here, pretty print the results and in there, you now don't start with the 34 00:02:18,640 --> 00:02:19,870 field you're looking for, 35 00:02:19,900 --> 00:02:26,800 so not rating average and then equal to something 36 00:02:27,050 --> 00:02:32,960 but instead you start directly with $or telling mongodb that you'll have multiple 37 00:02:32,960 --> 00:02:33,880 conditions. 38 00:02:34,070 --> 00:02:39,380 You then add an array and that array will hold all the conditions that mongodb will check. Now 39 00:02:39,410 --> 00:02:47,330 or means that it will return results that match any of these conditions, or means an alternative, 40 00:02:47,330 --> 00:02:56,360 so we then specify our filters which we previously specified directly inside find, like rating.average 41 00:02:56,450 --> 00:03:03,400 being lower, whoops dollar sign lower than five 42 00:03:03,580 --> 00:03:03,970 ok. 43 00:03:03,970 --> 00:03:09,080 So this is the expression we previously would add directly inside find 44 00:03:09,280 --> 00:03:15,310 but now it's just one of two expressions or two checks we want to perform and therefore, it goes into 45 00:03:15,370 --> 00:03:17,870 this or array. 46 00:03:17,950 --> 00:03:24,100 Now the second value in or then is the second expression and we can have more than 2, you can have 3, 47 00:03:24,120 --> 00:03:26,530 5, as many as you want essentially. 48 00:03:26,560 --> 00:03:29,320 So here, rating.average, 49 00:03:29,650 --> 00:03:35,170 that is my other check if it's greater than 9.3. 50 00:03:35,320 --> 00:03:44,320 So now I got two checks here and this showed me find all documents with a rating lower down 5 or greater 51 00:03:44,320 --> 00:03:45,590 than 9.3 52 00:03:45,700 --> 00:03:47,780 but nothing in-between. 53 00:03:48,160 --> 00:03:51,400 If I now hit enter, I do find some results, 54 00:03:51,460 --> 00:03:53,830 here we have a very good rating, 55 00:03:53,860 --> 00:03:57,620 the next result has a very poor rating, below 5, 56 00:03:57,670 --> 00:04:03,160 then if I scroll up, I get a very good rating again and there should be one last document and there is 57 00:04:03,480 --> 00:04:06,340 with a very poor rating and that's it, 58 00:04:06,340 --> 00:04:10,810 these are all the documents that were returned, which makes sense because if you remember, when I ran 59 00:04:10,810 --> 00:04:13,630 these queries standalone, we had two and two, 60 00:04:13,660 --> 00:04:23,490 now we have four. So that is the OR operator, now the nor operator is pretty self-explanatory, 61 00:04:23,650 --> 00:04:32,980 if I add nor here, it simply means return me all documents where neither of the two conditions is met. 62 00:04:32,980 --> 00:04:38,970 So in our case it should give us all documents with a rating that is not lower than 5, 63 00:04:39,070 --> 00:04:46,980 so basically higher than 5 but also give me all documents with a rating that is not higher than 9.3. 64 00:04:47,020 --> 00:04:49,220 If I execute this, I get quite a lot of documents 65 00:04:49,270 --> 00:04:55,820 and now let's do a quick check to see if that yields us the difference to the last query. For this, 66 00:04:55,840 --> 00:04:59,500 first of all let's find all documents, 67 00:04:59,590 --> 00:05:02,380 so we have 240 in the collection. 68 00:05:02,380 --> 00:05:05,990 Then let's run our query with or, so what we previously had 69 00:05:06,240 --> 00:05:08,040 and let's count the results there, 70 00:05:08,050 --> 00:05:16,900 here we've got four. So the nor query should now essentially give us if we replace pretty with count, 236 71 00:05:17,010 --> 00:05:23,680 and that is the case. And this kind of shows what nor does, it's the inverse of this check here 72 00:05:24,040 --> 00:05:29,560 and that gives you a lot of utility of course for narrowing down your set to the data you require.