Get startedGet started for free

Sorting

1. Sorting

We've learned how to project out only the fields we need from a query. There are other conditions we can give to MongoDB about the data returned. In this lesson, we'll learn how to sort results on the server before they get returned to us.

2. Sorting post-query with Python

First, its useful to review how we can sort a list of retrieved documents with only Python. This may be plenty performant, especially for small datasets. After all, we can store a local cache of our data and try many different operations on it. Let's get a list of all physics prize documents, with the year field projected. I'm not interested in printing out the full projected documents with the document ids. So, I'll use a list comprehension to collect the year values, slice out the first five values, and print them. It looks like the documents are in reverse chronological order, but we get no guarantee of this. To sort the documents in ascending order of year, I use the built-in Python "sorted" function. I also import the "itemgetter" function from Python's standard library. Given a key, it fetches the value for that key in a dictionary. To sort in reverse - or descending - order, we can pass True as the "reverse" keyword argument to the "sorted" function.

3. Sorting in-query with MongoDB

We can also ask Mongo to do simple sorting by field values on the server and yield results in sorted order. Here, we pass a "sort" argument to the "find" method, giving a list of field-direction pairs. In this case, we want to sort on the "year" field, and in the ascending direction. I don't need the documents beyond the print statement, so I pass the cursor to the list comprehension. To sort by year in descending order, we use negative one as the second element of the sort pair. Why is a list passed to the sort keyword argument? This is because you can sort first by one field and then by others. Let's see how.

4. Primary and secondary sorting

Let's sort prize documents first by ascending year and then by descending category. To do this, we provide the corresponding pairs in order as a list. Here, we also query for prizes awarded between 1966 and 1970, exclusive. We project out only the data we need, the category and year values. Notice that we could sort by fields that we do not project - in this case, we happen to be also projecting the sort fields. For each projection yielded by the cursor, we format a string with the year and category value. To do this, we use Python's double-star dictionary unpacking syntax. The output shows years increasing and, for each year, categories decreasing. In both cases, the order is alphabetical because the fields are both string-valued. For the four-digit-year strings, sorting produces the same result as numerical sorting. We can see that there was no award for economics in 1967 or 1968, and there was no award for peace in 1967.

5. Sorting with pymongo versus MongoDB shell

One last thing: the command-line shell for MongoDB uses JavaScript. You specify a sort using the form of a JavaScript object, which looks like a Python dictionary. This works because JavaScript objects in the console keep their key order as entered. In Python 3-point-6 and below, though, there is no similar guarantee with dictionaries. The order of keys may not be preserved as entered. This is why pymongo requires a list of tuples.

6. Let's get sorted!

Let's get some practice with sorting.