MongoDB Sort Documents

You can sort documents of a MongoDB query using sort() method.

MongoDB Sort Documents – To sort documents in a collection based on a field, use cursor.sort() method. Sort method accepts Field and Order pairs in a document as argument. Field indicates that sorting of documents will occur based on the field specified and Order specifies the sorting order.

In this tutorial, we shall learn to sort documents in a collection based on a field in ascending or descending order using cursor.sort() method with examples.

Syntax of sort() method

The syntax of sort() method is

cursor.sort({field:order, field:order [, field:order] })

field [String] : any field name

order [Number] : 1 or -1 is allowed

OrderDescription
1Ascending (Increasing) Order
-1Descending (Decreasing) Order

Document to the sort() method is mandatory. But field:order pairs are optional. Which means, at the least sort() method expects an empty document.

Note : db.collection.find() returns cursor to the records. And sort() method can be applied on this cursor to sort the documents in increasing or decreasing order :

db.collection.find().sort({FIELD:ORDER})

Example 1 – Sort Documents

In this example, we will sort the documents in resulting query, in ascending order, based on the field "age".

> db.customers.find().sort({"age":1});
{ "_id" : ObjectId("59edffea5f82df4555f2bfac"), "name" : "Midhuna", "age" : 23 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfaf"), "name" : "Manju", "age" : 23 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfad"), "name" : "Akhil", "age" : 24 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfb0"), "name" : "Bharat", "age" : 24 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfb1"), "name" : "Arya", "age" : 25 }

Documents are sorted in ascending order based on the field specified.

Example 2 – sort() Default Behaviour

In this example, we will call sort() method on a query, with no argument specified. By default the documents are ordered by _id in ascending order.

> db.customers.find().sort({});
{ "_id" : ObjectId("59edffea5f82df4555f2bfac"), "name" : "Midhuna", "age" : 23 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfad"), "name" : "Akhil", "age" : 24 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfaf"), "name" : "Manju", "age" : 23 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfb0"), "name" : "Bharat", "age" : 24 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfb1"), "name" : "Arya", "age" : 25 }

The documents are sorted in the ascending order of _id.

Example 3 – Sort with Multiple Fields

In this example, we will sort the documents based on multiple fields and their specified respective order. We have given two fields “age” and “name” and specified their respective orders to sort() method.

> db.customers.find().sort({"age":1,"name":1});
{ "_id" : ObjectId("59edffea5f82df4555f2bfaf"), "name" : "Manju", "age" : 23 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfac"), "name" : "Midhuna", "age" : 23 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfad"), "name" : "Akhil", "age" : 24 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfb0"), "name" : "Bharat", "age" : 24 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfb1"), "name" : "Arya", "age" : 25 }
> db.customers.find().sort({"age":1,"name":-1});
{ "_id" : ObjectId("59edffea5f82df4555f2bfac"), "name" : "Midhuna", "age" : 23 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfaf"), "name" : "Manju", "age" : 23 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfb0"), "name" : "Bharat", "age" : 24 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfad"), "name" : "Akhil", "age" : 24 }
{ "_id" : ObjectId("59edffea5f82df4555f2bfb1"), "name" : "Arya", "age" : 25 }

The command db.customers.find().sort({“age”:1,”name”:1});  first sorted the documents based on age in ascending order, then the documents with same age value are sorted in ascending order based on “name”.

The command db.customers.find().sort({“age”:1,”name”:-1});  first sorted the documents based on age in ascending order, then the documents with same age value are sorted in descending order based on “name”.

Conclusion

In this MongoDB Tutorial – MongoDB Sort Documents, we have learnt to sort documents of a collection based on fields in ascending or descending order.