Sort
Overview of sort document
Last updated
Overview of sort document
Last updated
Sort document allows you to specify how results returned from find type responses should be ordered. You can sort results in ascending or descending order for any field.
A format of a sort document is shown below. You provide a field, which is a field saved inside a database, and a value which can be either 1 or -1 (ascending or descending).
An example is shown below. Here we have two fields which we know exists in database. This will sort all the records first by name ascending, then by age descending.
To sort items inside an object, separate fields by a dot.
Sort does not work with arrays that have more than one item in array. This includes fields inside nested forms (collections). If you are sure that your nested forms won't have more than one item at a time you can use the dot separator show above.
If your arrays have more than one item, you would need to use an aggregate request. (Not supported yet).
If sort cannot be done using indexes, then sorting will be done in memory which requires that the size of records being sorted do not exceed 32 megabytes. If it exceeds, you will get an error. We provide an ability to add indexes on your collections to avoid this. How to add indexes and how to ensure that your sort documents will use those indexes read in a page provided below.
For other resources that you get from our services we provide our own indexes.
As CodeMash is using MongoDB database internally, the following resources explain more on how to use sort.