Articles → MONGODB → $Sort Operator In Mongodb

$Sort Operator In Mongodb






Purpose





Syntax


db.[collection].aggregate([
  { $sort: { field1: 1 } }
])




  1. 1 means sorting in ascending order
  2. -1 means sorting in descending order

Example




Picture showing the sample mongodb collection




db.mycollection.aggregate([
  { $sort: { age: -1 } }
])



Output


Picture showing the output of $sort operator in mongodb


Picture showing the output of $sort operator in mongodb



Posted By  -  Karan Gupta
 
Posted On  -  Monday, April 1, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250