Implementing $sort in aggregation

Hi, there,

In Lecture: Cursor Methods and Aggregation Equivalents, Matt showed us we can use $sort in aggregation pipeline as:

  {"$sort": {"year": ASCENDING, "title": ASCENDING}}

However, we know find() implements sorting with a list of tuples because python cannot guarantee the order of items in dictionary. It means the driver could sort documents by title first, then by year. Does pymongo pipeline has some underlying mechanism to fix the order of items in dictionary? Thanks a lot.

Q Yang

Hello nayamama,

Great question. To clarify for other students, nayamama is asking “Sorting has an inherent order, but Python dictionaries are not ordered - so how are we sorting with a dictionary?”

The answer is that PyMongo changes the type of the dictionary so that it is ordered. When the following dictionary is passed to $sort:

{"year": ASCENDING, "title": ASCENDING}

PyMongo takes in a dictionary, and casts it as a new data structure called a SON, which maintains the ordering of the keys in the dictionary so sorting can be done correctly. This type-casting is possible because SON is a subclass of the Python dictionary.

If you want to dig deeper, you can find the source code for the SON data type on Github.

I hope this helps!




Thanks a lot. The information is very helpful.