I'm currently trying to use MongoDB Compass to query my collection. However, I seem to be only able to filter the data.
Is there any way for me to sort the data as well? I would like to sort my data in ascending order using one of my data fields.
If MongoDB Compass isn't the best way to order a collection, what other GUI could I use?
To sort documents in MongoDB, you need to use sort() method. The method accepts a document containing a list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending order while -1 is used for descending order.
Using the sort() method will increase the readability of a query, which leads to a better understanding of a given dataset. Not only that, sorted data will be used by developers to write more complex algorithms. (This article is part of our MongoDB Guide.
The default internal sort order (or natural order) is an undefined implementation detail.
Using MongoDB Compass 1.7 or newer, you can sort (and project, skip, or limit) results by choosing the Documents tab and expanding the Options.
To sort in ascending order by a field myField
, use { myField:1 }
. Any of the usual cursor sort() options can be provided, including ordering results by multiple fields.
Note: options like sort and skip are not available in the default Schema tab because this view uses sampling to find a random set of documents, as opposed to the Documents view which displays a specific query result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With