How to sort documents with MongoDB Sort

By using MongoDB Sort you can organize documents according to specific fields. You can view your output in either ascending or descending order.

What is MongoDB Sort?

By using the database management 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 collections 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 optimize the output, the system uses MongoDB Sort. This changes the order in which the documents are organized. 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 collection, there is another parameter. This is a document and determines 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 descending order. If you do not define this value, then MongoDB Sort will automatically output the documents in ascending order.

MongoDB Sort examples

To better illustrate 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 information 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" : "Nguyen", "purchases" : 4 }
>

Sort by name

You can use MongoDB Sort to put the client list in alphabetical 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" : "Nguyen", "purchases" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "name" : "Smith", "purchases" : 1 }
>
If you want to view the list in descending order, use MongoDB Sort as follows:
>db.clientlist.find().sort({name : -1})
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "name" : "Smith", "purchases" : 1 },
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "name" : "Nguyen", "purchases" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "name" : "Jones", "purchases" : 4 }
>

Sort by orders

If you want to organize 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" : "Nguyen", "purchases" : 4 },
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "name" : "Smith", "purchases" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "name" : "Jones", "purchases" : 1 }
>
Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top