Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: What is Eve?
Lecture: Exploring Eve: Sorting

Login or purchase this course to watch this video and the rest of the course contents.
0:00 And what about sorting? Of course you can sort too, you just have to use the sort keyword in your query definition,
0:07 so for example, here we are sorting by location.city and then by last name, and because I am using a dash before the last name,
0:18 field I want then reversed. So basically I am looking for all the people with no filter, because I am not using the sorted by location.city
0:30 and last name reversed. Let's see what happens, we have 7 documents, the first document here has no location subdocument as you can see
0:41 the second is the same, no location, the third one is in Ashfield and then we have another person living in Auburne,
0:57 and then New York comes, and then San Francisco, San Francisco again and if we check it on the last names,
1:04 we will see that they are sorted by diverse order. For example, we have 2 people living in San Francisco let's go and see, we have miss Julia Red first
1:15 and then comes Miss Serena Love, so yes, we are getting them in reverse order, of course, we can also mix a filter with sorting,
1:25 so here we might go with something like where— okay, so here we are looking for all the people living in San Francisco
1:44 sorted by last name reversed. Only 2 documents, and we can see they both live in San Francisco and the first one has last name Red
2:02 and then Love, again, if I remove the dash here I am sorting by last name and we should get the same two documents
2:11 but the first one is Love this time and the second is Red. Also, when using sort you might want to switch to a Mongo syntax
2:23 something like this, which isn't probably very intuitive, but you can do that if you want to,
2:40 same result, of course.

Talk Python's Mastodon Michael Kennedy's Mastodon