Open In App

How to Sort a Collection by Date in MongoDB?

Last Updated : 05 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In MongoDB, sorting collections by date is a common requirement for many applications. Sorting by date allows us to display data in reverse order, making it easier for users to understand and analyze. In this article, we will explore different approaches to sorting collections by date in MongoDB, using various techniques and methods along with examples and so on.

How to Sort by Date in MongoDB?

In many applications, especially those dealing with timeseries data or event logs, there is a common need to sort documents based on their date. For example, consider an application that tracks user activities such as logging in, making purchases or interacting with content. Being able to sort these activities can provide valuable output into user behavior over time. Below are the two methods in the MongoDB that allow to sort a collection by date are as follow:

  1. Sort by Date Ascending
  2. Sort by Date Descending

Let’s set up an environment to sort a collection by date in MongoDB.

To understand How to sort a collection by date in MongoDB we need a document on which we will perform various operations and queries. Here we will consider a collection of events that consists of the below records into it. So we have considered a collection named “events” containing documents with a “timestamp” field representing the date of the event.

db.events.insertOne({event: "Geeks Event 1", timestamp: new Date("2024-01-20")})
db.events.insertOne({event: "Geeks Event 2", timestamp: new Date("2024-01-21")})
db.events.insertOne({event: "Geeks Event 3", timestamp: new Date("2024-01-22")})
db.events.insertOne({event: "Geeks Event 4", timestamp: new Date("2024-01-23")})
db.events.insertOne({event: "Geeks Event 5", timestamp: new Date("2024-01-24")})

1. Sort by Date Ascending

We use the sort() method with a value of 1 for the date field to sort documents in ascending order by date.

Example:

In this example, a collection named “events” contains documents with a “timestamp” field representing the date of the event. The query retrieves documents from the “events” collection and sorts them in ascending order by the “timestamp” field.

db.events.find().sort({"timestamp": 1})

Output:

out_1_mgdb_sort

Sort by Date Ascending

Explanation: In the above Query, We retrieves documents from the events collection and sorts them in ascending order based on the timestamp field.

2. Sort by Date Descending

We use the sort() method with a value of ‘-1‘ for the date field to sort documents in descending order by date.

Example:

In this example, The query retrieves documents from the “events” collection and sorts them by the “timestamp” field in descending order.

db.events.find().sort({"timestamp": -1})

Output:

out_2_mgdb_sort

Sort by Date Descending

Explanation: In the above Query, We retrieves documents from the events collection and sorts them in descending order based on the timestamp field.

Conclusion

Overall, sorting collections by date in MongoDB is important for applications to handle ime-sensitive data. Whether you need to display events or analyze trends over time, sorting by date provides valuable insights. By using the sort() method with either ascending or descending order, you can easily organize your data to meet your applications requirements. Understanding these sorting techniques in MongoDB can greatly enhance your ability to manage and analyze date-based data effectively.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads