MongoDB provides a few ways to sort documents in a particular order. Each of these allow us to order the documents in ascending or descending order.
There’s also the option to order the results of a $text
search – sorting by the computed textScore
metadata in descending order.
Here are 3 ways to sort documents in MongoDB.
The cursor.sort()
Method
The cursor.sort()
method specifies the order in which the query returns matching documents.
Suppose we have a collection called employees
with the following documents:
{ "_id" : 1, "name" : "Bob", "salary" : 55000 } { "_id" : 2, "name" : "Sarah", "salary" : 128000 } { "_id" : 3, "name" : "Fritz", "salary" : 25000 } { "_id" : 4, "name" : "Christopher", "salary" : 45000 } { "_id" : 5, "name" : "Beck", "salary" : 82000 } { "_id" : 6, "name" : "Homer", "salary" : 1 } { "_id" : 7, "name" : "Bartholomew", "salary" : 1582000 } { "_id" : 8, "name" : "Zoro", "salary" : 300000 } { "_id" : 9, "name" : "Xena", "salary" : 382000 }
Here’s an example of returning those documents sorted by their salary
field in ascending order, and also their _id
field:
db.employees.find().sort( { salary: 1, _id: 1 } )
Result:
{ "_id" : 6, "name" : "Homer", "salary" : 1 } { "_id" : 3, "name" : "Fritz", "salary" : 25000 } { "_id" : 4, "name" : "Christopher", "salary" : 45000 } { "_id" : 1, "name" : "Bob", "salary" : 55000 } { "_id" : 5, "name" : "Beck", "salary" : 82000 } { "_id" : 2, "name" : "Sarah", "salary" : 128000 } { "_id" : 8, "name" : "Zoro", "salary" : 300000 } { "_id" : 9, "name" : "Xena", "salary" : 382000 } { "_id" : 7, "name" : "Bartholomew", "salary" : 1582000 }
Here, we used 1
for ascending order. If we wanted it in descending order, we’d simply use -1
instead.
The reason I included the _id
field in the ordering is because, if two or more employees have the same salary, then the _id
field can be used to sort those documents between themselves.
While this may seem trivial, it will prevent MongoDB from returning those documents in a different order each time we run the query.
You can also sort the results of a $text
search. See MongoDB sort()
for an example.
The $sort
Aggregation Pipeline Stage
When using the aggregation pipeline framework, you can use the $sort
stage to sort all input documents and returns them to the pipeline in sorted order.
Here’s how we can use $sort
to return the same result as the previous example. Except, let’s sort them in descending order this time:
db.employees.aggregate(
[
{ $sort : { salary : -1, _id: 1 } }
]
)
Result:
{ "_id" : 7, "name" : "Bartholomew", "salary" : 1582000 } { "_id" : 9, "name" : "Xena", "salary" : 382000 } { "_id" : 8, "name" : "Zoro", "salary" : 300000 } { "_id" : 2, "name" : "Sarah", "salary" : 128000 } { "_id" : 5, "name" : "Beck", "salary" : 82000 } { "_id" : 1, "name" : "Bob", "salary" : 55000 } { "_id" : 4, "name" : "Christopher", "salary" : 45000 } { "_id" : 3, "name" : "Fritz", "salary" : 25000 } { "_id" : 6, "name" : "Homer", "salary" : 1 }
You can also sort the results of a $text
search. See MongoDB $sort
for an example.
The $orderBy
Query Modifier
The $orderBy
query modifier can be used in one of two forms.
Like this:
db.employees.find( { $query: {}, $orderBy: { salary: 1 } } )
Or like this::
db.employees.find()._addSpecial( "$orderby", { salary : 1 } )
The $orderBy
query modifier has been deprecated in the mongo
shell since v3.2, but I decided to include it here anyway. See the MongoDB documentation for more information.