By using MongoDB Sort you can organise documents according to specific fields. You can view your output in either ascending or des­cend­ing order.

What is MongoDB Sort?

By using the database man­age­ment system MongoDB, you can save data in the form of documents in databases which you create using MongoDB Create Database. Documents can also be collated within col­lec­tions and indexed to get a better overview and create a logical order. The aim of this system is not only to save the data but also to make them available at every moment. To optimise the output, the system uses MongoDB Sort. This changes the order in which the documents are organised. This makes lists and queries quicker and easier to use.

Tip

You can find many other commands in our overview at MongoDB commands.

Syntax and how to use MongoDB Sort

Alongside MongoDB Find, MongoDB Sort is something you will use often when working with a database. They can also be used together. The basic syntax looks like this:

db.collection_name.find().sort({field_name: sort_order})

Together with the actual MongoDB Sort command which must contain the name of the col­lec­tion, there is another parameter. This is a document and de­term­ines the fields by which the system should sort the documents. What’s more, you can set the sort order by changing the sort_order parameter: You can do this by using the value 1 for ascending order and -1 for des­cend­ing order. If you do not define this value, then MongoDB Sort will auto­mat­ic­ally output the documents in ascending order.

MongoDB Sort examples

To better il­lus­trate MongoDB Sort and the different ways in which it can be used, here are a few simple examples. Let’s take a client list, which has three entries and in­form­a­tion on the client’s name and the number of orders. This is how the list looks without being sorted:

>db.clientlist.find()
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "name" : "Smith", "purchases" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "name" : "Jones", "purchases" : 1 },
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "name" : "Singh", "purchases" : 4 }
>
Tip: Managed MongoDB from IONOS

Managed MongoDB from IONOS enables you to con­cen­trate on the es­sen­tials. From in­stall­a­tion to operation and main­ten­ance work, IONOS makes sure you always get the best per­form­ance from your data banks.

Sort by name

You can use MongoDB Sort to put the client list in al­pha­bet­ic­al order. For our example it would look as follows:

>db.clientlist.find().sort({name : 1})
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "name" : "Jones", "purchases" : 4 },
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "name" : "Singh", "purchases" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "name" : "Smith", "purchases" : 1 }
>

If you want to view the list in des­cend­ing order, use MongoDB Sort as follows:

>db.clientlist.find().sort({name : -1})
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "name" : "Smith", "purchases" : 1 },
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "name" : "Singh", "purchases" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "name" : "Jones", "purchases" : 4 }
>

Sort by orders

If you want to organise your client list by how often they make orders and those with the most purchases, MongoDB Sort will look as follows:

>db.clientlist.find().sort({purchases : -1})
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "name" : "Singh", "purchases" : 4 },
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "name" : "Smith", "purchases" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "name" : "Jones", "purchases" : 1 }
>
Go to Main Menu