Articles → MONGODB → $Sort Operator In Mongodb
$Sort Operator In Mongodb
Purpose
Syntax
db.[collection].aggregate([
{ $sort: { field1: 1 } }
])
- 1 means sorting in ascending order
- -1 means sorting in descending order
Example
db.mycollection.aggregate([
{ $sort: { age: -1 } }
])
Output