So let's talk a little bit more about queries and the
shell, and how use the shell to do them, and some of the
things you might want to do, such as sorting.
So if we flip over to the shell I have a running, we
have our products collection, a pretty short collection.
But let's look at a couple things.
So first of all, let's look at the sorting.
Let's do the following, let's get all the products, and
let's just get a couple fields though to kind of keep the
display output small for our small window here.
There they are, and as we see, some of them have prices.
So if you want to sort in Mono, you would add dot sort
after your find statement.
So sorting in the shell, db dot collection name dot find,
some parameters to find that indicate our query, and maybe
the field selection also, optionally dot sort, and then
we have an expression here.
And this expression is just our sort criteria.
And this is in, what I like to call, a
Mongo key pattern notation.
And these Mongo key patterns are used for a few things.
They're used both to indicate sort order, they're also used
to declare indexes, and they're also used to declare
shard keys.
So it's going to come up a little bit
as a recurring thing.
And they are expressed as JSON.
So our where criteria here, when we do the query, would be
JSON, as we've talked about previously.
Also then, this key pattern for the sort
order is also JSON.
And in general what that pattern is going to look like
is going to be kind of field name comma direction.
Where field name is just the name your field, and direction
is either one or minus 1.
One being descending order minus 1 being descending
order, so it just indicates which direction
you'd like to go.
So let's look at an example.
Let's do a sort here on price, and that's it.
So this is our key pattern, and we're going to order by
price ascending.
And there we have it, so we see the order is by
price, 12, 19, 220.
Now something else interesting to note here though is not all
of these products have a price, right?
Some things like service packages, right,
you're billed monthly.
So one time price is not meaningful for those.
There those products do not have that.
They have some other fields then in talking about their
monthly recurring prices, if we look at them.
And this sample data you'll import in the homework, so you
take more of a look at it there.
So in this case because the price field is not present in
those documents, price evaluates to
null for those documents.
And we just simply sort based on that.
And null is different data type than the number.
So in Mongo, there's an arbitrary but consistent
ordering of values of different types.
So null is always less than all numbers, right, in Mongo.
So we're going to get this particular order.
And that ordering isn't terribly meaningful in that
case, but the important thing is that it's consistent.
And maybe the even more important thing is that you
don't get an error back here, right?
Now if you only wanted items have a price, we
can easily do that.
We could do something like this, and we're all set.
So we're only going to get the products where the price field
is presents.
So sorting on a field will not do any filtering even if the
field name is not present in a particular document in the
collection.
It's somewhat analogous, I'd say, to in a relational
database a column, which has null values.
A little different, but somewhat analogous.
So here I filtered out the ones where
there's no price at all.
Now what if we want to sort in descending order instead of
ascending order?
In that case, what we will do is we will use minus 1 here.
So that number there had a meaning in this case, which is
to indicate the direction.
So we can do that too, and now we have them
in descending order.
In Mongo we use a lot of JSON.
So for these things here, such as query expressions, key
patterns, also for something called commands, which we're
going to talk about in a different
section in a future week.
In general, often these things--
this has a meaning here, right?
This indicates our stored order.
This actually-- these ones here have a meeting too.
We could have said zero here indicating I want you to leave
out price rather than include it, right?
So that was meaningful too.
Sometimes we have a right-hand side value like colon one,
just because we have to to make the JSON legal, right?
So if we think about JSON, you typically have a field name f,
and then some value here on the right-hand side, right?
Well we can't just leave this out, or it's
not valid JSON, right?
This is not JSON, right?
So we can't do that.
But we could do this.
So you'll sometimes see that notion in Mango.
So that could be non-intuitive at first, so
just be aware of that.
Although right now we are not seeing a ton of that.
I think, perhaps, you could argue that this exists true,
has that notion to it a bit.
Although exists false could be used also, I believe.
So in a way, it does have a meaning.
So one other thing on sorting is we can do
multiple sort keys.
So for example, if we wanted to sort on last name, first
name, we could do something like--
obviously not for this collection.
Imagine actually we had a collection of customers or
something, and we want to list them alphabetically, last name
first, then first name.
I could do get me all the customers from the customer's
collection, give me their documents ordered by last name
comma first name.
So in SQL, this to be a lot like order by
last name, first name.
And we could also have a different sort direction for
these different fields that we are going to sort on.
So let's take a look at that, and see if we come up with an
example where that's meaningful.
We might want to look at, for example, let's say go back to
our blog post example.
Suppose we want to list all the blog post, and we want to
list them by author, and then for each author in order from
newest to oldest.
So if we had a blog post collection, you might do
author and then date posted minus 1.
In other words, give me the newest ones first.
So there's a compound kind of sort key, or order by key,
where one is ascending and one is descending.
And in this section we're not going to talk about indexes.
But if you have an index declared that is of relevance,
Mongo will use it for the sort.
So much like a traditional database, the behavior is
somewhat similar.