With comparison operators covered, let's have a look at logical operators, and, not, nor and or.
Now I will say it right away, the operator you'll probably use most often here is $or and I'll
explain why too.
So let's dive into our database or into our data sets here again, into our collection
and let's work with these operators.
So for that, let's quickly have a look at our structure of one document and let's say we want to
find all documents where or all items here where the rating is either lower than three or greater than 9,
so the very bad or the very high quality items.
Now I'll run each query standalone to begin with, so db.movies find pretty and then let's add the filter,
now I'm looking for rating.average and then here let's first of all check if there are any items
with a rating lower than 3
and there are not,
so let's modify this slightly, 4 nope,
5
ok.
So we get items, yeah
more than one
we're rating lower than five, two items to be precise.
You can by the way find out how many items there are by replacing pretty with count
and you'll just get back the number of items but more on that later.
So we have two items,
now let's check how many items or if we got any items with a value, a rating greater than nine.
Let me,
we got some items,
let me quickly check the amount, seven,
so let's maybe narrow this down even more,
9.5 too much, 9.4,
9.3,
ok two items.
So now let's combine these two, lower than, there we had five
and greater than 9.3.
Now for that, we can use the OR operator,
so I can write a new query here, pretty print the results and in there, you now don't start with the
field you're looking for,
so not rating average and then equal to something
but instead you start directly with $or telling mongodb that you'll have multiple
conditions.
You then add an array and that array will hold all the conditions that mongodb will check. Now
or means that it will return results that match any of these conditions, or means an alternative,
so we then specify our filters which we previously specified directly inside find, like rating.average
being lower, whoops dollar sign lower than five
ok.
So this is the expression we previously would add directly inside find
but now it's just one of two expressions or two checks we want to perform and therefore, it goes into
this or array.
Now the second value in or then is the second expression and we can have more than 2, you can have 3,
5, as many as you want essentially.
So here, rating.average,
that is my other check if it's greater than 9.3.
So now I got two checks here and this showed me find all documents with a rating lower down 5 or greater
than 9.3
but nothing in-between.
If I now hit enter, I do find some results,
here we have a very good rating,
the next result has a very poor rating, below 5,
then if I scroll up, I get a very good rating again and there should be one last document and there is
with a very poor rating and that's it,
these are all the documents that were returned, which makes sense because if you remember, when I ran
these queries standalone, we had two and two,
now we have four. So that is the OR operator, now the nor operator is pretty self-explanatory,
if I add nor here, it simply means return me all documents where neither of the two conditions is met.
So in our case it should give us all documents with a rating that is not lower than 5,
so basically higher than 5 but also give me all documents with a rating that is not higher than 9.3.
If I execute this, I get quite a lot of documents
and now let's do a quick check to see if that yields us the difference to the last query. For this,
first of all let's find all documents,
so we have 240 in the collection.
Then let's run our query with or, so what we previously had
and let's count the results there,
here we've got four. So the nor query should now essentially give us if we replace pretty with count, 236
and that is the case. And this kind of shows what nor does, it's the inverse of this check here
and that gives you a lot of utility of course for narrowing down your set to the data you require.