MongoDb Tutorial

Day 9 : Sort records in MongoDB








Mongodb tutorial series

Overview

In this part of the Learn Mongo Series, we will learn how to sort records in a collection in mongodb. We will be sorting records in ascending as well as descending order.

Let's sort

In mongodb we can sort the records using the sort command. The syntax for sort command is given below : db.COLLECTION_NAME.find().sort() . We can sort the records in ascending or descending order. Let's understand them one by one.

  • Sorting records in ascending order : We can pass a key parameter with the sort method. If the value passed witin the key is 1 , then the record is sorted in ascending order. Although the default order is also ascending order so if nothing is passed as key, then also collection is sorted in ascending order. An example is given below.
    Consider the following collection. Here pretty() is used to display the data in a presentable manner :
    										
    > db.details.find().pretty()
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
            "age" : "11",
            "description" : "learn mongodb",
            "name" : "Updated a"
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
            "name" : "nodejsera",
            "age" : "10",
            "description" : "Mongodb tuttorial series"
    }
    {
            "_id" : ObjectId("5996f7c3a4b26dc0d6d75c1f"),
            "name" : "b",
            "age" : "20",
            "description" : "this is B"
    }
    
    
    
    
    										
    									

    Let's sort them on the basis of age field :
    										
    > db.details.find().sort("age" : 1}).pretty()
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
            "name" : "nodejsera",
            "age" : "10",
            "description" : "Mongodb tuttorial series"
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
            "age" : "11",
            "description" : "learn mongodb",
            "name" : "Updated a"
    }
    {
            "_id" : ObjectId("5996f7c3a4b26dc0d6d75c1f"),
            "name" : "b",
            "age" : "20",
            "description" : "this is B"
    }
    
    
    										
    									

    It was not required to mention the key field , if we want to sort the data in ascending order.
  • Sorting records in descending order : We can pass a key parameter with the sort method. If the value passed witin the key is -1 , then the record is sorted in descending order. An example is given below.

    										
    > db.details.find().sort({"name" : -1}).pretty()
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1d"),
    		"name" : "Up",
            "age" : "11",
            "description" : "learn mongodb"
            
    }
    {
            "_id" : ObjectId("5996be4fa4b26dc0d6d75c1c"),
            "name" : "nodejsera",
            "age" : "10",
            "description" : "Mongodb tutorial series"
    }
    
    {
            "_id" : ObjectId("5996f7c3a4b26dc0d6d75c1f"),
            "name" : "b",
            "age" : "20",
            "description" : "this is B"
    }
    >
    										
    										
    									

    Here the collection is sorted in the descending order on the basis of name field.
  • Hurray !! Mission Accomplished. We have successfully sorted a collection in mongodb.

Summary

In this part of learn mongo series , we learned about how we can sort a collection in mongodb in ascending and descending order. We learned the following commands of mongodb :

  1. .pretty() : This method is used to display the data in presentable form.
  2. .sort({"key"}) This command is used to sort the collection in ascending order.
  3. .sort({"key" : 1}) Again this command is used to sort the collection in ascending order.
  4. .sort({"key" : -1}) This command is used to sort the collection in descending order.