Instructor: Let's now implement result sorting.
In order to enable our users to sort result
based on a certain field.
So, let's now take a look at all our results here for now.
All right, and so let's say that we actually want
to sort these results, let's say by price.
Okay, so starting with the lowest price and then moving up
all the way to the highest price.
All right, so we have all kinds of prices.
397, 497, 1197, 1497, 997.
We have even almost 3,000 here.
Then 1497 and so they're not really in order,
and so let's allow the user to basically sort these results
based on a field that can be passed using the query string.
So let's say we want to sort by price.
All right.
So if we run this now, of course, nothing will happen,
it will still show the exact same nine results
and in the same order, because before in the last lecture,
we filtered out basically this sort field.
Right?
And so let's now actually go ahead and use it.
All right?
So, remember here how we created the query object
and then excluded the sort field here,
so that it didn't pollute our filtering, right?
But now we actually need it and so let's now use it.
So, the third feature here is sorting.
Or actually, well that's the second one,
so let's just call this one 1A and 1B.
All right?
So, let's say that if there actually is a sort,
sort req.query.sort.
Okay, and you see it down here.
Now it's gone because of this error here, but anyway.
From our last request we got, of course,
there's a query object here and so in there
we had the sort property, all right?
And if there is a sort property,
well, that means that then
we want to actually sort the results based on the value.
Now one quick thing that we need to do,
is to change this query of variable here from
a constant to a normal variable.
So let, okay?
'Cause now we actually want to chain something
to this query.
So, the new query will be query.sort.
And we want to sort it by req.query.sort.
All right, 'cause that will be the value of the field,
so in this case price,
and Mongoose will then automatically sort the result
based on the price.
And now in case you're wondering,
why it works this way, so why we did this here,
just remember what we talked about over the last lectures.
So, remember how this tour.find here
is gonna return a query, right?
And so we stored that query object here
in this variable, so that later on
we can keep chaining more methods to it.
So more of these methods that are available
on all documents created through the query class.
Okay, remember that?
I think we talked about that in depth two lectures ago.
So in case this is still a bit confusing to you,
then just go back there
or you can also take a look at the documentation, all right?
Now let's actually test this out.
So sort is still set to price.
So I'll send it now.
Let's take a look at the result.
And indeed we start with the lowest price,
but I think we already had that before.
So we start with 397, then 497, then 997, 1197,
1497, and you see that the prices actually keep increasing.
So 1997, 2997.
And so yeah, indeed, they are now sorted.
Okay?
So we sorted them by the price in an ascending order.
But we can also sort them in a descending order.
That's very simple.
All we have to do is to set as the minus here.
So, minus price and Mongoose will then automatically
sort them in the other order.
So, in the descending order.
So let's test out that one as well.
And so now we should start with, yeah exactly,
with this one.
So, the first one is now the highest
and the last one is the 397 one.
Cool.
But we can actually take it one step further,
because we have many cases here which have the same price.
But how are these results with the same price then
ordered within them?
So let's say we have this one with this price
and then this one with the same price.
So why does this one appear before the second one?
The same thing with this one here maybe,
oh actually not.
So this one is just the only one with 1997.
But then here again we have these two with the same price.
We have, okay, they are all different.
Okay, these are all different.
But we want to rank them, basically,
according to a second criteria.
So in case there is a tie,
then we want to have a second field
by which we can then sort where the first one is the same.
So in this case the price.
So in Mongoose that's quite easy,
and let me show that to you here as a comment for example.
So in Mongoose we would just say sort
and then right now we have the price,
so we have it just like this,
but then in Mongoose all we have to do
is to add another field name to the string.
So for example, rating average.
Or I think it's ratings.
Okay, and so we now want to pass this field here as well
to our sort.
Okay?
Now we cannot leave a space here in the URL
and so instead we're gonna add a comma.
Okay?
So we want to sort first by price
and then as a second criteria, also by ratings average.
Okay?
And so now all we need to do
is to basically replace this comma here with a space.
All right?
And of course, we could have even more fields
by which we sort.
Okay?
But for now let's just leave it like this
and implement it very quickly in our code.
All right?
And actually let me create a new variable here.
Let's call it sort by.
And so we have req.query.sort
and what we're gonna do is to now split it by the comma.
So split the string by the comma,
and so this will then return an array of all the strings,
so all the field names,
and then all we have to do is to put it back together
using join, and as the join string we use a space.
All right?
And so just to make sure that it's correct.
Console.log, sort by, and then of course here
we want to use that sort by variable.
Okay?
So let's take a look at that.
And.
So 4.7 here, and the other one also has 4.7, all right?
And so let's actually go ahead and use our API
to change this ratings average here.
Okay?
So just as a fun experiment,
let's copy this ID here, then update tour,
and so let's come here to the body,
and so I want to update ratings average to 4.9.
Okay?
Let's send that, and indeed, you see that it got updated.
And so now let's change that again,
or let's try that again actually.
And so now you see the first one still has 4.7
and this one here now has 4.9.
And so they are now ordered with an ascending rating.
Right?
And of course, we could put a minus here
and so then they should change positions,
meaning that the one with 4.9 should appear first.
Let's try that as well.
And yeah, indeed.
So now this one, the sports lover tour,
is the one that appears first.
All right, great.
So it looks like it's working.
Let's a take a look at our results here.
And yeah, indeed.
And let's just take a look at what we logged here
at the console.
And so, yeah, this is our sort by variable, which yeah.
Which kind of looks similar to what we have here.
And so that was the goal of creating this variable.
Great, so that's now working.
Let's get rid of this console and this one here as well.
And all right.
And just to finish here,
let's actually add a default one.
And we do that by simply adding an else block here.
So in case that the user does not specify
any sort field in the URL query string,
we're still gonna add a sort to the query.
So query.sort and we will then sort
by the created add field, all right?
And actually in a descending order,
so that the newest ones appear first.
So minus created at.
All right?
Give this a save.
And so yeah, I think like this, we're done.
Let's just turn off this option here as well.
And so now we just are back to our regular URL
without any query strings.
And so let's take a look
if this is now actually ordered by the created at.
So this one here ends in 59 seconds and 30.
5930 as well.
And yeah.
So they were all created at the exact same moment,
because we imported them all at the same time.
All right?
And so in this case, we cannot really see
the effect of this piece of code
that we just added here, but in case these documents
were added at different times, then they would now
be ordered by the date they were created.
All right?
So that's it for sorting.
Next up, we're gonna talk about limiting the fields
that we want in our results.