MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
Lecture: Sorting MongoDB results
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
So, we're pretty good at finding and filtering down our result sets. The other super important things that databases do
0:08
is to sort them, put them in order, so I would like the best selling book and then the second best, and then the third best in this category,
0:16
that's a perfect sort by category, order by best selling this, right. So how do we do that in Mongo?
0:23
Let's go over here and it turns out that there's a sort that we can run, and the sort takes something, right, kind of like our projection does here,
0:31
so let me just show you before if I run this that this is not in order, so here we have c, c, d, f, and then t, p, w,
0:41
and eventually we're back just, you know, something before w, it is not sorted by title, not sorted by published date either,
0:48
these three seem to be descending but the next one is not, ok. So it's not sorted at all, it's just however it comes back,
0:54
probably by object id or something like this. Anyway, let's go and sort it, so let's suppose I would like sorted by title;
1:01
so very much like our filter thing or maybe even closer, actually, like our projection here is I can come say I would like to sort
1:10
and then this part that goes here, this one is ascending, right, so something that is positive means ascending, if it were negative,
1:17
it would mean go in reverse order. So let's run this, now you can see, actually this is the beginning of the title,
1:23
this exclamation mark and then some other exclamation marks, and then let's get past the symbols, a lot of symbols, anyway,
1:31
you can see this is sorted by this, sorted by the title, not sorted by date, 1994, 1993, 1996, we can also sort by date, let's comment this out,
1:42
say .sort, published, let's sort in reverse order, newest which was 2050, I think we might have been fooling around with that
1:53
or no actually I don't know where those came from. Anyway, 2050, 2038, 2037, 2030 and so on. Obviously, sorted in reverse order.
2:02
What if I want to sort by the title and then any time the title matches I want to see the newest one of those.
2:09
We can do that as well, so very very similarly we can say sort and then we just give it one of these objects with multiple values,
2:17
so you want to sort by title, there's your sort by title ascending and then after that, if any of the titles match,
2:27
let's show the newest one first, so sort by title ascending and then published descending, let's try that.
2:34
Great, ok so here notice that these titles are the same, you might have noticed that before, but here's 1994 and here's 1993,
2:41
so any time the title matches, we get the newest one first, I don't know if any others are in here with title matches.
2:47
This first one must prove it right, this is how it works, sort by that and then by and you can have as many then buys as you like
2:55
and they can either be ascending or descending, so here we're sorting by title first and then by published.
3:00
The other thing that's important to notice is everything in MongoDB is case sensitive, when you're working with strings
3:06
so that's probably going to play into this somewhere along the way. All right, so sorting pretty straightforward, just use these field names
3:14
and then the direction you want to sort. The other thing that's worth paying attention to is you are going to want to make sure that you have an index
3:21
so this sorting is actually fast, and we'll talk about that when we get to the performance section.