Sort () method
To sort documents in MongoDB, you need to use the sort () method. The sort () method accepts a document that contains a list of fields together with their sort order. To specify sort order 1 and-1. 1 is used in ascending order, and-1 for descending order.
Grammar:
The basic syntax for the sort () method is as follows
>db. Collection_name.find (). Sort ({key:1})
Example
Consider the collection Myycol with the following data
{"_id": ObjectId (5983548781331ADF45EC5), "title": "MongoDB Overview""_id": ObjectId (5983548781331ADF45EC6) , "title": "NoSQL Overview""_id": ObjectId (5983548781331ADF45EC7), "title": "Yiibai Overview"}
The following example shows a file sorted in descending order by title.
>db.mycol.find ({},{"title": 1,_id:0}). Sort ({"title": -1}) {"title": "Tutorials Point Overview"} { "title": "NoSQL Overview"}{"title": "MongoDB Overview"}>
Note that if sort precedence is not specified, then the sort () method displays the document in ascending order.
MongoDB (11) MongoDB Sort Document