Sorting can happen either in ascending or descending order. MongoDB provides few ways to sort documents. We can use cursor sort method and $sort stage method in the aggregation framework. There is also $orderby operator which is now depreciated.
cursor.sort() Method: Specifies the order in which the query returns matching documents. You must apply sort() method to the cursor before retrieving any documents from the database.
db.customers.find({ }).sort({name: 1})
In the above query, We are sorting the customers based on name in ascending order.
$sort Stage Method: Its the same with cursor sort method. But we use this sorting method in aggregation framework.
db.customers.aggregate([{ $sort: {name: 1} }])