So I mentioned there are more operators, now
the best way to learn about them all and to always well have a look at them even if you're done with
the course is of course to dive into the official documentation. There in the docs,
if you check out the mongodb server,
you can learn more about the crud operations which also will be interesting but if you scroll down to
the reference there, you can click on operators and there right now in this module, we'll talk about the
query and projection operators. And there you got the different categories that exist,
here are the projection operators,
the rest are query operators.
And for example, you also see them here on the starting page, you'll see here is $gt
which we saw before.
Now there are a bunch of operators and I will walk through many many of them in this module to explain
how they work and what you can do with them,
so let's start with some comparison operators because this is of course a group of operators you'll
use quite a bit.
So let's go back to our database here and this is the query I ran before, runtime 60.
Now let's say we want to search for movies where the runtime is actually lower than 60,
we can do this by specifying a document as a value,
remember the slide because this will then tell mongodb that we're not looking for equality which
is just a default it assumes but that we now want to use a query operator.
Now as a side note, there is a
$eq operator
and if I set this to 60, this query here is exactly equivalent to this query.
So if I use runtime 60 like this or equal 60 does not make a difference,
so this is simply a shortcut, the other syntax is just a shortcut to this more verbose mode.
Now where it does make a difference because there is no equivalent is if you use something else than
$eq, for example $ne for not equal
and again, you find all these operators in the official docs.
So now we're using the not equal operator to find all series or movies which are not equal or where
the runtime is not equal to 60 minutes.
So if I hit enter here, I get a bunch of data,
let's make it more readable by adding pretty
and let's check one document here,
there we have a runtime of 30 indeed.
Well let's check another document,
there we have a runtime of 42, so also not 60
and let's check a third one, there
we have a runtime of 30. And you can check them all,
there will be no series or no item with a runtime of 60 due to this query we executed.
Now this is not equal to,
of course we might be interested in searching for any series that has a runtime lower than 40 minutes,
let's say.
So not just not equal
because that would also give us series
with 60 minutes but lower than, so 40 minutes, 42, 60 that we'll all be out of the game.
If we do that and let's again add pretty for a nicer output, we will see that there is a series with a
runtime of 30
and there is another item with a runtime of 30 and there should be another item with a runtime of 30.
So you won't find any value above 40 here and it just happens to be the case that series
typically have a runtime of 30, 42 or 60 minutes,
so there's little in-between
by the looks of it
but this is now the lower than operator. So with that, we already covered ne, gt and lower than.
There also is lower than or equal with lte,
so the difference here simply is that if I was looking for let's say 42 and there happened to be some
items with a runtime of 42, if I use lt, I will not find any runtime equal to 42
but if I repeat that same command with lte, which means lower than or equal to 42, then if we scroll through,
we will find some items with a runtime of 42.
Let me quickly search one, here is one,
so that's the difference.
Now it should be pretty clear what gt and lte and gte therefore do. With gt, we find series where the
runtime is greater than 42,
so now we'll only have some items with 60 minutes in there like this one and of course with gte, we find
well greater than or equal,
so now again we'll have 60 minutes
series like this one but we will also find some items with
42 minutes of runtime.
Now you might not find one with scrolling through the set but keep in mind because we get back a cursor,
we only see the first 20 results of our query,
so there might be no 42 minute series in there.
You will get back results greater than or equal to 42
with this query.