MongoDB for Developers with Python Transcripts
Chapter: MongoDB's shell and native query syntax
Lecture: Concept: Sorting MongoDB results

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's review sorting as a concept. So there's a sort function right on the result set on the cursor that comes back from find,
0:08 and the way it works is we pass it some prototypical json document; but now instead of equality meaning matching,
0:15 it means tell me the thing and the direction that you want to sort. So here we want to say sort all the books descending
0:22 show me the most recently published to the oldest, right, show me the most recent books basically.
0:28 Now this works pretty well, we could put anything that has a direction like a minus one, or one, I think you could even put higher multiples
0:34 like ten and 20, 50, -10, but use one and minus one, keep your sanity. So this works well for one field, if we want to sort just by published,
0:41 but if I want to sort by one thing, and then another, well we just put more into this document that we passed to sort,
0:47 so we're going to say sort by title ascending and then sort by published descending, we run this, we saw that we get the results in our demo,
0:55 first we sorted ascending by the title and any time they matched we sorted descending by the publish date.
1:03 So first the 1994, A Nutshell Handbook, and then the 1993 one.

Talk Python's Mastodon Michael Kennedy's Mastodon