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.
0:04 The other super important things that databases do
0:07 is to sort them, put them in order, so I would like the best selling book
0:12 and then the second best, and then the third best in this category,
0:15 that's a perfect sort by category, order by best selling this, right.
0:20 So how do we do that in Mongo?
0:22 Let's go over here and it turns out that there's a sort that we can run,
0:25 and the sort takes something, right, kind of like our projection does here,
0:30 so let me just show you before if I run this that this is not in order,
0:33 so here we have c, c, d, f, and then t, p, w,
0:40 and eventually we're back just, you know, something before w,
0:43 it is not sorted by title, not sorted by published date either,
0:47 these three seem to be descending but the next one is not, ok.
0:50 So it's not sorted at all, it's just however it comes back,
0:53 probably by object id or something like this.
0:56 Anyway, let's go and sort it, so let's suppose I would like sorted by title;
1:00 so very much like our filter thing or maybe even closer, actually,
1:05 like our projection here is I can come say I would like to sort
1:09 and then this part that goes here, this one is ascending, right,
1:13 so something that is positive means ascending, if it were negative,
1:16 it would mean go in reverse order.
1:18 So let's run this, now you can see, actually this is the beginning of the title,
1:22 this exclamation mark and then some other exclamation marks,
1:25 and then let's get past the symbols, a lot of symbols, anyway,
1:30 you can see this is sorted by this, sorted by the title, not sorted by date,
1:36 1994, 1993, 1996, we can also sort by date, let's comment this out,
1:41 say .sort, published, let's sort in reverse order,
1:47 newest which was 2050, I think we might have been fooling around with that
1:52 or no actually I don't know where those came from.
1:55 Anyway, 2050, 2038, 2037, 2030 and so on.
1:58 Obviously, sorted in reverse order.
2:01 What if I want to sort by the title and then any time the title matches
2:05 I want to see the newest one of those.
2:08 We can do that as well, so very very similarly we can say sort
2:14 and then we just give it one of these objects with multiple values,
2:16 so you want to sort by title, there's your sort by title ascending
2:22 and then after that, if any of the titles match,
2:26 let's show the newest one first, so sort by title ascending
2:30 and then published descending, let's try that.
2:33 Great, ok so here notice that these titles are the same,
2:36 you might have noticed that before, but here's 1994 and here's 1993,
2:40 so any time the title matches, we get the newest one first,
2:44 I don't know if any others are in here with title matches.
2:46 This first one must prove it right, this is how it works,
2:50 sort by that and then by and you can have as many then buys as you like
2:54 and they can either be ascending or descending,
2:57 so here we're sorting by title first and then by published.
2:59 The other thing that's important to notice is
3:02 everything in MongoDB is case sensitive, when you're working with strings
3:05 so that's probably going to play into this somewhere along the way.
3:09 All right, so sorting pretty straightforward, just use these field names
3:13 and then the direction you want to sort.
3:15 The other thing that's worth paying attention to is
3:18 you are going to want to make sure that you have an index
3:20 so this sorting is actually fast, and we'll talk about that
3:22 when we get to the performance section.