How to sort documents in MongoDB

Introduction :

We can easily sort the documents before retrieving them from a collection in MongoDB. Sorting is one of the important functionality that we should be aware of. In this tutorial, I will show you how to sort documents with a query.

Definition :

The sort method is defined as below :

cursor.sort(sort)

It takes only one parameter, sort, this is of type document that defines the sorting order of the final result. It contains the sorting field and the value for it, like below :

{ sort_field: value }

We can give either 1 or -1 as the value. 1 is for ascending order and -1 is for descending order.

Note that we can also pass multiple field-value pairs like below :

{ field_1: 1, field_2: -1}

Example :

Let’s try to learn it with an example. We will create one DB, one collection, put some documents in the collection and test the sort method on it.

Start mongod :

Open one terminal and run mongod command to start the mongoDB daemon. Now open one more terminal and use mongo command to enter into the mongoDB shell.

Make sure to do the above step before moving to the next steps.

Create one new DB :

Use the below command in the mongo shell to create one new database called school :

use school

Create one collection :

Create one collection called results in the database school with the below command :

db.createCollection("results")

Add documents to the collection :

Insert a few documents to the results collection :

db.results.insertMany([{"name": "Albert","paper" : "Science", "marks" : 88},{"name": "Bryan","pap
er" : "Science", "marks" : 78},{"name": "Rob","paper" : "Mathematics", "marks" : 68},{"name": "Bob"
,"paper" : "Science", "marks" : 91}])

MongoDB sort documents example

Now, our collection is ready with four different documents. Let’s use some queries to check how sorting works :

Get all data :

Get all the documents in the results collection :

db.results.find({})

It will return them in the same order we have inserted.

{ "_id" : ObjectId("5d5282071ceb9b762657eb37"), "name" : "Albert", "paper" : "Science", "marks" : 88 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb38"), "name" : "Bryan", "paper" : "Science", "marks" : 78 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb39"), "name" : "Rob", "paper" : "Mathematics", "marks" : 68 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb3a"), "name" : "Bob", "paper" : "Science", "marks" : 91 }

Get all data by sorting them based on marks :

db.results.find({}).sort({marks : 1})

1 indicates that we are sorting in ascending order. The result will be like below :

{ "_id" : ObjectId("5d5282071ceb9b762657eb39"), "name" : "Rob", "paper" : "Mathematics", "marks" : 68 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb38"), "name" : "Bryan", "paper" : "Science", "marks" : 78 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb37"), "name" : "Albert", "paper" : "Science", "marks" : 88 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb3a"), "name" : "Bob", "paper" : "Science", "marks" : 91 }

Sort in descending order :

Use -1 to sort in descending order :

db.results.find({}).sort({marks : -1})

Output :

{ "_id" : ObjectId("5d5282071ceb9b762657eb3a"), "name" : "Bob", "paper" : "Science", "marks" : 91 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb37"), "name" : "Albert", "paper" : "Science", "marks" : 88 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb38"), "name" : "Bryan", "paper" : "Science", "marks" : 78 }
{ "_id" : ObjectId("5d5282071ceb9b762657eb39"), "name" : "Rob", "paper" : "Mathematics", "marks" : 68 }

MongoDB sort documents example2

Try to sort data in the backend always. You may have different applications on different platforms like web, android, and iOS for the same server. Keeping the logical part in backend makes it easier to scale.