We get our flights in the database again
and now let me dive a bit deeper into how we can find data. Thus far,
we always used this way of finding data, find with no arguments.
Now just as you can filter for specific records or documents when deleting or updating, you can also
do so when finding.
So I can access my flight data here,
let me quickly clear this to make it a bit easier to read
and instead of passing nothing, you can pass a document to it which will be interpreted as a filter for
your collection and you can filter for anything that makes sense.
You could filter for a name equal to Max but of course this returns nothing because in our collection,
we get these two flights and there is no name field in there
but what we can do of course is we can use flight data and find all flights that are let's say intercontinental,
so we use that key here.
We do so by adding a document, then the name and this can be wrapped by quotation marks but it doesn't
have to
and then let's say I want to find all flights where this is true, this would be a valid filter. If I now
pretty print this which always is optional,
just to make it easier to read, if I hit enter,
now I get one flight and this is one flight which is an intercontinental flight.
Of course, this is a very useful feature because in pretty much every application you're creating,
you are not always interested in all your data but in a subset of the data which you need for that specific
webpage you're showing to the user or that specific part of your mobile app. You're also not limited
to simple queries like this,
we could also find all flights with a distance greater than ten thousand kilometers and for this, we
just need to modify the syntax a bit more.
It's easy to look for equal values, distance 12000 like this,
that would be simple and it gives us back this one flight here, just not pretty printed
but if we want to have a greater than check, we can do that too.
And there we just need to pass another document or object,
I'll use these words interchangeably,
we need to pass another object as a value for distance
and here we again use one of these special operators provided by mongodb, $gt which
stands for greater than and no worries,
you don't need to learn them by heart right now. Gt does exist, $gt and that means greater than
and then you enter the value which you want to have as a greater than value.
So here we could enter 10000
and let's also pretty print this to make
it a bit easier to read and we get this one flight which has a distance greater than 10000.
The other flight had a distance of 950 and therefore if I modify this to look for all flights with
a distance greater than 900, we actually get back both flights,
so this is of course another very useful feature.
Now let me close this lecture or end this lecture by showing you findOne with this example.
So I'm looking for flights with a distance greater than 900 kilometers, if I switch it from find to findOne
and I remove pretty here because pretty is not supported on findOne, I get back only one flight, the
first flight that matched my criteria and that makes sense because findOne only gives me back one
element.
So if I want to find all, I should use find, if I am only interested in the first matching element, findOne is the
solution.
Important is it will still respect that filter but then not look for all matching elements but only
the first matching element.