MongoDB vs SQL: Sorting

December 05, 2020


Recently, I had to add several API endpoints which provide pagination, filter and sort parameters to the client while using MongoDB as database. Implementing pagination and filtering wasn’t complicated however I hit a major setback when implementing sorting logic. The reason was that sorting is not enabled on virtual fields in MongoDB. This is as opposed to SQL where calculated fields can be sorted. For applications which have a lot of virtual fields the above-mentioned MongoDB missing feature can be a major disadvantage (for example if database data has a lot of fields which need to be translated based on user language).

Eventually, I ended up with creating a script which would populate the database with the virtual fields which is reasonable because the database is relatively small and is not expected to become very big. However, for applications with large databases this may not be a reasonable solution, of course. In that case perhaps migrating to an SQL based database may be needed.