w3resource

MongoDB sort results from collection

Description

In this page, we are going to discuss how to sort data for one or more specific columns in ascending or descending order.

Syntax:

collection.find().sort( {column1:1or -1 [, column2:1 or -1] });

Parameters:

Arguments Description
collection Name of the collection.
column1, column2 Name of the fields or columns.
1 or -1 indicates the order (1 for ascending and -1 for descending)

Our database name is 'myinfo' and our collection name is 'userdetails'. Here, is the collection bellow.

Sample collection "userdetails"

{
        "_id" : ObjectId("528cab88e1e41035b889f2bf"),
        "user_id" : "user1",
        "password" : "1a2b3c",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "CONSULTANT",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. Alex",
                "MR. Dang",
                "MR Haris"
        ],
        "community_members" : [
                700,
                200,
                1500
        ],
        "friends_id" : [
                "kumar",
                "harry",
                "anand"
        ],
        "ban_friends_id" : [
                "Amir",
                "Raja",
                "mont"
        ]
}
{
        "_id" : ObjectId("528cabb5e1e41035b889f2c0"),
        "user_id" : "user2",
        "password" : "11aa1a",
        "date_of_join" : "17/10/2009",
        "education" : "M.B.A.",
        "profession" : "MARKETING",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. Roy",
                "MR. Das",
                "MR Doglus"
        ],
        "community_members" : [
                500,
                300,
                1400
        ],
        "friends_id" : [
                "pal",
                "viki",
                "john"
        ],
        "ban_friends_id" : [
                "jalan",
                "monoj",
                "evan"
        ]
}
{
        "_id" : ObjectId("528cabd0e1e41035b889f2c1"),
        "user_id" : "user3",
        "password" : "b1c1d1",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "IT COR.",
        "interest" : "ART",
        "community_name" : [
                "MODERN ART",
                "CLASSICAL ART",
                "WESTERN ART"
        ],
        "community_moder_id" : [
                "MR. Rifel",
                "MR. Sarma",
                "MR Bhatia"
        ],
        "community_members" : [
                5000,
                2000,
                1500
        ],
        "friends_id" : [
                "philip",
                "anant",
                "alan"
        ],
        "ban_friends_id" : [
                "Amir",
                "Raja",
                "mont"
        ]
}
{
        "_id" : ObjectId("528cabece1e41035b889f2c2"),
        "user_id" : "user4",
        "password" : "abczyx",
        "date_of_join" : "17/8/2009",
        "education" : "M.B.B.S.",
        "profession" : "DOCTOR",
        "interest" : "SPORTS",
        "community_name" : [
                "ATHELATIC",
                "GAMES FAN GYES",
                "FAVOURIT GAMES"
        ],
        "community_moder_id" : [
                "MR. Paul",
                "MR. Das",
                "MR Doglus"
        ],
        "community_members" : [
                2500,
                2200,
                3500
        ],
        "friends_id" : [
                "vinod",
                "viki",
                "john"
        ],
        "ban_friends_id" : [
                "jalan",
                "monoj",
                "evan"
        ]
}

Document written in command prompt.

Fetch results by sorting one column in ascending order

If we want to fetch the result by sorting on "education" column in ascending order, the following mongodb command can be used :

>db.userdetails.find().sort({"education":1})

N.B. find() method displays the documents in a non-structured format but to display the results in a formatted way, the pretty() method can be used.

The SQL equivalent code is

SELECT * 
FROM userdetails 
ORDER BY education

Output:

{
        "_id" : ObjectId("528cabb5e1e41035b889f2c0"),
        "user_id" : "user2",
        "password" : "11aa1a",
        "date_of_join" : "17/10/2009",
        "education" : "M.B.A.",
        "profession" : "MARKETING",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. Roy",
                "MR. Das",
                "MR Doglus"
        ],
        "community_members" : [
                500,
                300,
                1400
        ],
        "friends_id" : [
                "pal",
                "viki",
                "john"
        ],
        "ban_friends_id" : [
                "jalan",
                "monoj",
                "evan"
        ]
}
{
        "_id" : ObjectId("528cabece1e41035b889f2c2"),
        "user_id" : "user4",
        "password" : "abczyx",
        "date_of_join" : "17/8/2009",
        "education" : "M.B.B.S.",
        "profession" : "DOCTOR",
        "interest" : "SPORTS",
        "community_name" : [
                "ATHELATIC",
                "GAMES FAN GYES",
                "FAVOURIT GAMES"
        ],
        "community_moder_id" : [
                "MR. Paul",
                "MR. Das",
                "MR Doglus"
        ],
        "community_members" : [
                2500,
                2200,
                3500
        ],
        "friends_id" : [
                "vinod",
                "viki",
                "john"
        ],
        "ban_friends_id" : [
                "jalan",
                "monoj",
                "evan"
        ]
}
{
        "_id" : ObjectId("528cab88e1e41035b889f2bf"),
        "user_id" : "user1",
        "password" : "1a2b3c",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "CONSULTANT",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. Alex",
                "MR. Dang",
                "MR Haris"
        ],
        "community_members" : [
                700,
                200,
                1500
        ],
        "friends_id" : [
                "kumar",
                "harry",
                "anand"
        ],
        "ban_friends_id" : [
                "Amir",
                "Raja",
                "mont"
        ]
}
{
        "_id" : ObjectId("528cabd0e1e41035b889f2c1"),
        "user_id" : "user3",
        "password" : "b1c1d1",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "IT COR.",
        "interest" : "ART",
        "community_name" : [
                "MODERN ART",
                "CLASSICAL ART",
                "WESTERN ART"
        ],
        "community_moder_id" : [
                "MR. Rifel",
                "MR. Sarma",
                "MR Bhatia"
        ],
        "community_members" : [
                5000,
                2000,
                1500
        ],
        "friends_id" : [
                "philip",
                "anant",
                "alan"
        ],
        "ban_friends_id" : [
                "Amir",
                "Raja",
                "mont"
        ]
}

The above output shows that all the documents have appeared with ascending order on "education" column.

N.B. Here in the example, a "1" after "education" have introduced for sorting in descending order on "education" column.

Document written in command prompt.

Fetch results by sorting one column in descending order

If we want to fetch the result by sorting on "education" column in descending order, the following mongodb command can be used :

>db.userdetails.find().sort({"education":-1})

The SQL equivalent code is

SELECT * 
FROM userdetails 
ORDER BY education DESC; 

Output:

{
        "_id" : ObjectId("528cab88e1e41035b889f2bf"),
        "user_id" : "user1",
        "password" : "1a2b3c",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "CONSULTANT",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. Alex",
                "MR. Dang",
                "MR Haris"
        ],
        "community_members" : [
                700,
                200,
                1500
        ],
        "friends_id" : [
                "kumar",
                "harry",
                "anand"
        ],
        "ban_friends_id" : [
                "Amir",
                "Raja",
                "mont"
        ]
}
{
        "_id" : ObjectId("528cabd0e1e41035b889f2c1"),
        "user_id" : "user3",
        "password" : "b1c1d1",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "IT COR.",
        "interest" : "ART",
        "community_name" : [
                "MODERN ART",
                "CLASSICAL ART",
                "WESTERN ART"
        ],
        "community_moder_id" : [
                "MR. Rifel",
                "MR. Sarma",
                "MR Bhatia"
        ],
        "community_members" : [
                5000,
                2000,
                1500
        ],
        "friends_id" : [
                "philip",
                "anant",
                "alan"
        ],
        "ban_friends_id" : [
                "Amir",
                "Raja",
                "mont"
        ]
}
{
        "_id" : ObjectId("528cabece1e41035b889f2c2"),
        "user_id" : "user4",
        "password" : "abczyx",
        "date_of_join" : "17/8/2009",
        "education" : "M.B.B.S.",
        "profession" : "DOCTOR",
        "interest" : "SPORTS",
        "community_name" : [
                "ATHELATIC",
                "GAMES FAN GYES",
                "FAVOURIT GAMES"
        ],
        "community_moder_id" : [
                "MR. Paul",
                "MR. Das",
                "MR Doglus"
        ],
        "community_members" : [
                2500,
                2200,
                3500
        ],
        "friends_id" : [
                "vinod",
                "viki",
                "john"
        ],
        "ban_friends_id" : [
                "jalan",
                "monoj",
                "evan"
        ]
}
{
        "_id" : ObjectId("528cabb5e1e41035b889f2c0"),
        "user_id" : "user2",
        "password" : "11aa1a",
        "date_of_join" : "17/10/2009",
        "education" : "M.B.A.",
        "profession" : "MARKETING",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. Roy",
                "MR. Das",
                "MR Doglus"
        ],
        "community_members" : [
                500,
                300,
                1400
        ],
        "friends_id" : [
                "pal",
                "viki",
                "john"
        ],
        "ban_friends_id" : [
                "jalan",
                "monoj",
                "evan"
        ]
}

The above output shows that, all the documents have appeared with ascending order on "education" column.

Document written in command prompt.

Fetch results by sorting on more than one column

If we want to fetch the result by sorting on "education" column in ascending order and "password" column in descending order whether two or more primary sort are same, the following mongodb command can be used :

>db.userdetails.find().sort({"education":1,"password":-1})

The SQL equivalent code is

SELECT * 
FROM userdetails 
ORDER BY education,password DESC; 

Output:

The above output shows that, all the documents have appeared with ascending order on "education" column and the "user_id" of last two documents are same. Then the "password" column for those rows have arranged in descending order.

{
        "_id" : ObjectId("528cabb5e1e41035b889f2c0"),
        "user_id" : "user2",
        "password" : "11aa1a",
        "date_of_join" : "17/10/2009",
        "education" : "M.B.A.",
        "profession" : "MARKETING",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. Roy",
                "MR. Das",
                "MR Doglus"
        ],
        "community_members" : [
                500,
                300,
                1400
        ],
        "friends_id" : [
                "pal",
                "viki",
                "john"
        ],
        "ban_friends_id" : [
                "jalan",
                "monoj",
                "evan"
        ]
}
{
        "_id" : ObjectId("528cabece1e41035b889f2c2"),
        "user_id" : "user4",
        "password" : "abczyx",
        "date_of_join" : "17/8/2009",
        "education" : "M.B.B.S.",
        "profession" : "DOCTOR",
        "interest" : "SPORTS",
        "community_name" : [
                "ATHELATIC",
                "GAMES FAN GYES",
                "FAVOURIT GAMES"
        ],
        "community_moder_id" : [
                "MR. Paul",
                "MR. Das",
                "MR Doglus"
        ],
        "community_members" : [
                2500,
                2200,
                3500
        ],
        "friends_id" : [
                "vinod",
                "viki",
                "john"
        ],
        "ban_friends_id" : [
                "jalan",
                "monoj",
                "evan"
        ]
}
{
        "_id" : ObjectId("528cabd0e1e41035b889f2c1"),
        "user_id" : "user3",
        "password" : "b1c1d1",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "IT COR.",
        "interest" : "ART",
        "community_name" : [
                "MODERN ART",
                "CLASSICAL ART",
                "WESTERN ART"
        ],
        "community_moder_id" : [
                "MR. Rifel",
                "MR. Sarma",
                "MR Bhatia"
        ],
        "community_members" : [
                5000,
                2000,
                1500
        ],
        "friends_id" : [
                "philip",
                "anant",
                "alan"
        ],
        "ban_friends_id" : [
                "Amir",
                "Raja",
                "mont"
        ]
}
{
        "_id" : ObjectId("528cab88e1e41035b889f2bf"),
        "user_id" : "user1",
        "password" : "1a2b3c",
        "date_of_join" : "16/10/2010",
        "education" : "M.C.A.",
        "profession" : "CONSULTANT",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. Alex",
                "MR. Dang",
                "MR Haris"
        ],
        "community_members" : [
                700,
                200,
                1500
        ],
        "friends_id" : [
                "kumar",
                "harry",
                "anand"
        ],
        "ban_friends_id" : [
                "Amir",
                "Raja",
                "mont"
        ]
}

Document written in command prompt.

Previous: MongoDB field selection
Next: MongoDB skip() and limit()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/mongodb-sort-results.php