TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

MongoDB sort()

MongoDB sort() method for beginners and professionals with examples on CRUD, insert document, query document, update document, delete document, use database, projection etc.

<< Back to MONGODB

MongoDB sort() method

In MongoDB, sort() method is used to sort the documents in the collection. This method accepts a document containing list of fields along with their sorting order.

The sorting order is specified as 1 or -1.

  • 1 is used for ascending order sorting.
  • -1 is used for descending order sorting.

Syntax:

db.COLLECTION_NAME.find().sort({KEY:1})

Scenario

Consider an example which has a collection name TheDeveloperBlog.

This collection has following fields within it.

    [
      {
        Course: "Java",
        details: { Duration: "6 months", Trainer: "Sonoo Jaiswal" },
        Batch: [ { size: "Medium", qty: 25 } ],
        category: "Programming Language"
      },
      {
        Course: ".Net",
        details: { Duration: "6 months", Trainer: "Prashant Verma" },
        Batch: [ { size: "Small", qty: 5 }, { size: "Medium", qty: 10 }, ],
        category: "Programming Language"
      },
      {
        Course: "Web Designing",
        details: { Duration: "3 months", Trainer: "Rashmi Desai" },
        Batch: [ { size: "Small", qty: 5 }, { size: "Large", qty: 10 } ],
        category: "Programming Language"
      }
    ];

Execute the following query to display the documents in descending order.

db.TheDeveloperBlog.find().sort({"Course":-1})

This will show the documents in descending order.

{ "_id" : ObjectId("564dbced8e2c097d15fbb603"), "Course" : "Web Designing", "det
ails" : { "Duration" : "3 months", "Trainer" : "Rashmi Desai" }, "Batch" : [ { "
size" : "Small", "qty" : 5 }, { "size" : "Large", "qty" : 10 } ], "category" : "
Programming Language" }
{ "_id" : ObjectId("564dbced8e2c097d15fbb601"), "Course" : "Java", "details" : {
 "Duration" : "6 months", "Trainer" : "Sonoo Jaiswal" }, "Batch" : [ { "size" :
"Medium", "qty" : 25 } ], "category" : "Programming Language" }
{ "_id" : ObjectId("564dbced8e2c097d15fbb602"), "Course" : ".Net", "details" : {
 "Duration" : "6 months", "Trainer" : "Prashant Verma" }, "Batch" : [ { "size" :
 "Small", "qty" : 5 }, { "size" : "Medium", "qty" : 10 } ], "category" : "Progra
mming Language" }

Note: By default sort() method displays the documents in ascending order. If you don't specify the sorting preference, it will display documents in ascending order.


Next TopicQuery Modifiers




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf