So I'm back in the shell
and the important thing is we'll work with movies which we imported in the last course module,
so make sure you got that data there
and if I can quickly findOne, that already is our first read method. findOne does what the name suggests,
it finds exactly one document, you can pass a filter to it and we learn about filters later to define
which one document that should be
but then it will always find the first matching document,
if you specify nothing, it will look through all documents and simply give you back the first document
in your collection,
if you specified some filter, like age 30 which will not work here because we have no age field but if
you specify something like this, it would give you the first document where this condition is met,
so empty curly braces or no curly braces at all will simply give you back the first document.
Please note that this also does not give you back a cursor but only one document which it instantly
loads.
Now this is the shape of the data in our movies collection,
this is data about TV series and maybe we should have named it series therefore but now it's movies
and there you see
we've got a couple of fields, like the name, which type it is, language,
some genres, if it's still running or already ended, the runtime when it premiered
and well that should be all pretty self-explanatory,
there is a bunch of data for us to play around with and that is exactly what we'll do.
So this was findOne,
now let's say we want to find one document but we want to find one different document. For that,
let's first of all have a look at the alternative to findOne which is find, now find like this will
give us back a cursor as you learned and we'll care about cursors later,
for now let's just accept it, pretty is a method we can change to get a prettier output.
Now this theoretically gives us all documents but since it gives us a cursor in the end, it does not
give us all documents to avoid querying the database or a collection for all documents, in the shell,
we just get back the first 20 documents. So with find, we see there's a bunch of stuff in there
and for example, there is The Last Ship series and why don't we tried to find this with either find
one or find. Well to do this,
we will need a filter and we specify a filter on both findOne and find, it doesn't matter,
they work in exactly the same way regarding all these options you learned about by passing a document
to the method, so that again is the same for findOne and find, it
doesn't matter,
the only thing that does change of course is that findOne will give us back one document that meets
that criteria,
find will give us back all documents that meet that criteria.
Now here I want to check for the name,
so for the name field to be equal to The Last Ship, we can do this by specifying the key here, name,
so the field where we want to look into and then the value and by default,
if you specify it like this, so could just key value, mongodb
will look for equality.
So if now hit enter, we get all documents where the name is The Last Ship which happens to only be
one document,
we of course would get more documents if we used a field where the value would be repeating,
so for example if we use runtime 60 by quickly changing that to runtime and then that is a number, so 60,
then you see these are way more documents,
obviously it's a wall of text,
we could use pretty to make it easier to read but the main thing is these are multiple documents. Now
that also allows me to highlight the difference with findOne, findOne only gives us back one document,
the first document that meets that criteria which is Glee and not The Last Ship because that happens
to be the first document
having the right runtime. So this is findOne and find and how you pass a filter by passing a document
as an argument and that document describes your query selector,
so your filtering criteria you could say. Here we use equality but of course as I already mentioned, there
are way more ways of filtering and way more operators.
So let's dive into these operators.