Sorting (order by) documents in MongoDB

You are here : Home / MongoDB Tutorial


Contents of page >

Sorting (order by) documents in MongoDB


First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1,  firstName:"ankit"})
> db.employee.insert({_id : 2,  firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3,  firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4,  firstName:"neh", salary : 3000 })
First line above will create table (or collection) (if table already exists it will insert documents in it).


1) Sort documents of collection on basis of salary in ascending order in MongoDB>
We will use find() and sort() method>
> db.employee.find().sort({salary : 1})
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }


2) Sql query equivalent to above MongoDB query is >
select * from employee
order by salary;

3) Sort documents of collection on basis of salary in descending order in MongoDB>
We will use find() and sort() method>
> db.employee.find().sort({salary : -1})
Output>
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 1, "firstName" : "ankit" }


4) Sql query equivalent to above MongoDB query is >
select * from employee
order by salary desc;


5) Sort documents of collection on basis of firstName, salary in ascending order in MongoDB>
We will use find() and sort() method>
> db.employee.find().sort({ firstName : 1, salary : 1})
Output>
{ "_id" : 1, "firstName" : "ankit" }
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }

6) FIND Example > Sort documents of collection on basis of salary in ascending order in MongoDB where salary > 1000 >
We will use find() and sort() method>
> db.employee.find({ salary : {$gt : 1000} }).sort({salary : 1})
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }


7) Sort documents of collection on basis of salary in descending order in MongoDB where salary > 1000 >
We will use find() and sort() method>
> db.employee.find({ salary : {$gt : 1000} }).sort({salary : -1})
Output>
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }





8) SUMMARY>
So in this mongoDB tutorial we learned how to Sort (order by) documents in MongoDB.
1) FIND Example > Sort documents of collection on basis of salary in ascending order in MongoDB>
> db.employee.find().sort({salary : 1})


3) FIND Example > Sort documents of collection on basis of salary in descending order in MongoDB>
> db.employee.find().sort({salary : -1})


5) FIND Example > Sort documents of collection on basis of firstName, salary in ascending order in MongoDB>
> db.employee.find().sort({ firstName : 1, salary : 1})


6) FIND Example > Sort documents of collection on basis of salary in ascending order in MongoDB where salary > 1000 >
> db.employee.find({ salary : {$gt : 1000} }).sort({salary : 1})


7) FIND Example > Sort documents of collection on basis of salary in descending order in MongoDB where salary > 1000 >
> db.employee.find({ salary : {$gt : 1000} }).sort({salary : -1})

Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

What is MongoDB - A quick introduction to database


eEdit
Must read for you :