So let's go ahead and look at get movies.
This is in db.py, and here you can see the get movies function.
Now let's go ahead and copy this and put it in our Jupyter Notebook so
you can actually play around with the function a little bit.
So the first things I'm going to do here is define the parameters that we go ahead
and pass in to get movies.
So this is a very simple example, we're not making a text search or
searching by genre.
So our filter's just going to be an empty dictionary.
And, we want this query to be for the homepage.
So we're going to say page is = to 0.
And just like on the homepage, we're going to render 20 movies per page.
Now I've gone ahead and simplified the get movies function a little bit, by removing
the function and kind of splitting up the code across multiple cells,
so that we can more easily see what's happening.
And in this first cell, this is where we ensure that the homepage is
filled with movies familiar to our users.
So by default, when we query MongoDB,
the documents are returned without any particular order.
But to make sure that these movies are relevant to our users,
here we're going to sort them by the number of reviews they have.
Now obviously, there are many different ways that we could implement this but
this naive approach works pretty well for this example application.
And as you can see, our sort key uses dot notation.
And that's because we're sorting on a value that is
nested through a series of nested documents.
And this is the exact same dot notation that we use when we're filtering for
embedded documents.
In this case, we're now using it for sorting.
So very simply, we just pass in our filters to find, and
then we append the sort method, passing our sort key.
And then, of course, similar to how we do sorting in aggregation,
we also have to pass a direction.
So we want to sort things so that the most reviews are at the front, so
we're going to sort this descending.
And then second thing we want to do is go ahead and get the total movie count.
So this query is going to currently return all of the movies, and so
when we run count you can see that, indeed, it does return all of them.
And so the first thing this query does is go ahead and
counts how many documents are on this cursor.
And we go ahead and store that in this variable called total num movies.
And this is one of the return values of the get movies function.