Aggregation $sort
This aggregation stage groups sorts all documents in the specified sort order.
Note: Remember that the order of your stages matters. Each stage only acts upon the documents that previous stages provide.
Example
db.listingsAndReviews.aggregate([
{
$sort: { "accommodates": -1 }
},
{
$project: {
"name": 1,
"accommodates": 1
}
},
{
$limit: 5
}
])
This will return the documents sorted in descending order by the accommodates field.
Note: The sort order can be chosen by using
1or-1.1is ascending and-1is descending.