AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

MongoDB Sort: list and sort your documents

PARTAGEZ

MongoDB Sort allows you to sort documents based on specific criteria. The result is displayed in ascending or descending order.

What is MongoDB Sort?

The MongoDB database management system allows you to store your data in the form of documents in databases to be created with the MongoDB Create Database command. For a better overview and a logical order, the documents are grouped and indexed in collections. The objective of this system is not only to store data, but also to make them available at any time. The MongoDB Sort method allows to optimize the output in the document-oriented database. This adapts the classification of the output. This makes it possible to use lists and queries even faster and more efficiently.

Advice

Find the main commands of the management system in our overview of MongoDB commands.

MongoDB Sort: Syntax and Operation

Along with MongoDB Find, MongoDB Sort is a frequently used method when working with databases. It is also possible to combine the two. Here is how the basic syntax looks like:

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

In addition to the MongoDB Sort command, which must always include the collection name, there is yet another parameter. This is a document that will determine the criteria for sorting documents by the system. You can set the meaning of sorting by adapting the sort_order parameter: the value 1 means ascending sort, -1 descending sort. If this value is not set, MongoDB Sort will display documents in ascending order by default.

Examples for MongoDB Sort

To better illustrate MongoDB Sort and its different possibilities, let’s use some simple examples. Let’s imagine a list of customers that, for clarity, has three entries and contains information about the customer’s name and the number of orders. Here’s what that list would look like without sorting:

>db.listeclients.find()
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "nom" : "Martin", "commandes" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "nom" : "Richard", "commandes" : 4 },
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "nom" : "Picard", "commandes" : 1 }
>

Sort by name

To sort the client list alphabetically, use MongoDB Sort. Here’s what it would look like in our example:

>db.listeclients.find().sort({nom : 1})
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "nom" : "Martin", "commandes" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "nom" : "Picard", "commandes" : 1 },
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "nom" : "Richard", "commandes" : 4 }
>

To sort the names in descending order, use MongoDB Sort as follows:

>db.kundenliste.find().sort({nom : -1})
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "nom" : "Richard", "commandes" : 4 },
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "nom" : "Picard", "commandes" : 1 },
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "nom" : "Martin", "commandes" : 2 }
>

Sort by orders

If you want to classify your customers according to their order frequency starting with the one who orders the most, the syntax of MongoDB Sort will be as follows:

>db.kundenliste.find().sort({commandes : -1})
{ _id : ObjectID ( "409f288c184f9132cd772c2" ), "nom" : "Richard", "commandes" : 4 }
{ _id : ObjectID ( "409f288c184f9132cd772c1" ), "nom" : "Martin", "commandes" : 2 },
{ _id : ObjectID ( "409f288c184f9132cd772c3" ), "nom" : "Picard", "commandes" : 1 },
>

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact