MongoDB sort

an introduction
MongoDB sort() method
Use the sort() method to sort data in MongoDB. The sort() method can specify the sorted field through parameters, and use 1 and -1 to specify the sorting method, where 1 is for ascending order, and -1 is used for descending sort.
 
two grammar
The basic syntax of the sort() method is as follows:
>db.COLLECTION_NAME.find().sort({KEY:1})
 
Three examples
The following example demonstrates that the data in the col collection is sorted by the field likes in descending order:
  1. >db.col.find({},{"title":1,_id:0}).sort({"likes":-1})
  2. { "title" : "PHP 教程" }
  3. { "title" : "Java 教程" }
  4. { "title" : "MongoDB 教程" }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327094515&siteId=291194637