Table of Contents
MongoDB is a popular NoSQL database management system that allows for efficient storage and retrieval of large volumes of documents. One useful feature of MongoDB is the ability to sort documents based on their date. This can be achieved by specifying the desired date field and using the “sort()” method in the MongoDB query language. The documents will then be arranged in ascending or descending order based on the date values in the specified field. This functionality is particularly beneficial for applications that require the organization and retrieval of time-sensitive data. By sorting documents based on their date, users can easily access and analyze the most recent or oldest data, facilitating effective decision-making and data management.
MongoDB: Sort Documents By Date
You can use the following methods to sort documents by a date field in MongoDB:
Method 1: Sort by Date Ascending
db.sales.find().sort({"date_field": 1})
Method 2: Sort by Date Descending
db.sales.find().sort({"date_field": -1})
The following examples show how to use each method with a collection sales with the following documents:
db.sales.insertOne({day: new Date("2020-01-20"), amount: 40})
db.sales.insertOne({day: new Date("2020-01-21"), amount: 32})
db.sales.insertOne({day: new Date("2020-01-22"), amount: 19})
db.sales.insertOne({day: new Date("2020-01-23"), amount: 29})
db.sales.insertOne({day: new Date("2020-01-24"), amount: 35})Example 1: Sort by Date Ascending
We can use the following code to sort all of the documents by date in ascending order:
db.sales.find().sort({"day": 1})
This query returns the following results:
{ _id: ObjectId("6189401696cd2ba58ce928fa"),
day: 2020-01-20T00:00:00.000Z,
amount: 40 }
{ _id: ObjectId("6189401696cd2ba58ce928fb"),
day: 2020-01-21T00:00:00.000Z,
amount: 32 }
{ _id: ObjectId("6189401696cd2ba58ce928fc"),
day: 2020-01-22T00:00:00.000Z,
amount: 19 }
{ _id: ObjectId("6189401696cd2ba58ce928fd"),
day: 2020-01-23T00:00:00.000Z,
amount: 29 }
{ _id: ObjectId("6189401696cd2ba58ce928fe"),
day: 2020-01-24T00:00:00.000Z,
amount: 35 }Notice that the document with the oldest date (2020-01-20) appears first while the document with the most recent date (2020-01-24) appears last.
Example 2: Sort by Date Descending
We can use the following code to sort all of the documents by date in descending order:
db.sales.find().sort({"day": -1})
This query returns the following results:
{ _id: ObjectId("6189401696cd2ba58ce928fe"),
day: 2020-01-24T00:00:00.000Z,
amount: 35 }
{ _id: ObjectId("6189401696cd2ba58ce928fd"),
day: 2020-01-23T00:00:00.000Z,
amount: 29 }
{ _id: ObjectId("6189401696cd2ba58ce928fc"),
day: 2020-01-22T00:00:00.000Z,
amount: 19 }
{ _id: ObjectId("6189401696cd2ba58ce928fb"),
day: 2020-01-21T00:00:00.000Z,
amount: 32 }
{ _id: ObjectId("6189401696cd2ba58ce928fa"),
day: 2020-01-20T00:00:00.000Z,
amount: 40 }Notice that the document with the most recent date (2020-01-24) appears first while the document with the oldest date (2020-01-20) appears last.
Note: You can find the complete documentation for the sort function .
Additional Resources
The following tutorials explain how to perform other common operations in MongoDB:
Cite this article
stats writer (2024). How can documents be sorted in MongoDB based on their date?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-documents-be-sorted-in-mongodb-based-on-their-date/
stats writer. "How can documents be sorted in MongoDB based on their date?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-documents-be-sorted-in-mongodb-based-on-their-date/.
stats writer. "How can documents be sorted in MongoDB based on their date?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-documents-be-sorted-in-mongodb-based-on-their-date/.
stats writer (2024) 'How can documents be sorted in MongoDB based on their date?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-documents-be-sorted-in-mongodb-based-on-their-date/.
[1] stats writer, "How can documents be sorted in MongoDB based on their date?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.
stats writer. How can documents be sorted in MongoDB based on their date?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
