Sorting MongoDB Data Based on Date: A Guide

Feedback

MongoDB belongs to the NoSQL category of databases and thus it stores data in JSON format. In database management systems, the sorting phenomenon is used to retrieve documents in a sequential manner. The sort by date support of MongoDB helps to get the documents that are arranged according to the date field.

Sorting can be executed in either ascending or descending order. Similarly, the functionality of sorting by date permits the retrieval of documents in both orders. Upon completion of this post, you will be capable of implementing the sort by date feature in MongoDB.

How do you sort documents by date in MongoDB

In this segment, we’ll cover the available methods for sorting MongoDB documents by date. MongoDB’s “$sort” operator and “sort()” method both provide date sorting functionality. Consequently, the following approaches can be used to retrieve documents based on their date field:

The given below is the syntax for utilizing the sort() method.

db.collection-name.find

(


)

.sort

(


<

date-field

>

:

<

order

>


)

To utilize the $sort operator, you can employ the aggregation() method and apply the following syntax within it.

db.collection-name.aggregate

(


{


$sort

:

{


<

date-field

>

:

<

order

>


}


}


)

The instances utilized in the aforementioned syntaxes are as follows:

  • This method requires the name of the collection in which it should be applied.
  • The field name that contains the date function should be used instead of “date-field”.
  • The “order” tag denotes the arrangement, which can be interchanged with either “1” to indicate ascending order or “-1” to indicate descending order.

In the following section, multiple instances will be presented to illustrate how to utilize the “sort()” method and “$sort” operator for sorting documents based on their dates.

Retrieving documents in sort by date order

To begin with, we can examine the records in our “staff” collection, which reveal that there is a common field called “JoiningDate” present in each document.


>

db.staff.find

(


)

.pretty

(


)

Example: Using the sort() method

By using the “sort()” method syntax, you can sort the documents in ascending order based on the “JoiningDate” field, which indicates the date of staff member joining.


>

db.staff.find

(


)

.sort

(


{


“JoiningDate”

:

1


}


)

.pretty

(


)

You can obtain the results in descending order by using the following command:


>

db.staff.find

(


)

.sort

(


{


“JoiningDate”

: –

1


}


)

.pretty

(


)

Example: Using $sort operator

The aforementioned example uses documents from the “staff” collection. To sort the documents based on the “JoiningDate” field, the aggregate method’s $sort operator can be used with the following commands.

To arrange in ascending order, make use of the command provided:


>

db.staff.aggregate

(


{


$sort

:

{

JoiningDate:

1


}


}


)

.pretty

(


)

To arrange in a descending order, utilize the given command below:


>

db.staff.aggregate

(


{


$sort

:

{

JoiningDate: –

1


}


}


)

.pretty

(


)

In case you use a value other than (-1 or 1) to specify the order, please note that an error similar to the one displayed in the output below may occur.


>

db.staff.aggregate

(


{


$sort

:

{

JoiningDate:

2


}


}


)

.pretty

(


)

Conclusion

MongoDB offers a wide range of operators and methods to store and
retrieve documents from a collection
. The sorting of MongoDB documents can be done in various ways, such as sorting by date to arrange documents in ascending/descending order based on their date field. In this MongoDB tutorial post, we have provided multiple approaches to sorting documents by date. The guide includes examples that demonstrate the utilization of the $sort operator with the aggregate method and the sort() method, both of which are used to retrieve documents based on the date function.

Frequently Asked Questions