EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB sort by date

MongoDB sort by date

MongoDB sort by date

Definition of MongoDB sort by date

MongoDB sort by date is used to sort the date field in ascending or descending order, we can sort the date by using the sort method in MongoDB. We can also use the aggregate method to sort the date and timestamp field in MongoDB. We need to pass -1 or 1 value with the date field to sort the data. If we pass -1 value with date field our result shows in descending order, if we pass 1 value with date field our result shows in ascending order.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Below is the syntax of sort by date in MongoDB.

1) db.collection_name.find ().sort ( { name_of_date_field : 1 (Display the date in ascending order)} )

2) db.collection_name.find ().sort ( { name_of_date_field : -1 (Display the date in descending order) } )

3) db.collection_name.aggregate ( { $sort: { name_of_date_field : -1 (Display the date in descending order) } } )

4) db.collection_name.aggregate ( { $sort: { name_of_date_field : 1 (Display the date in ascending order) } } )

Parameter description syntax of MongoDB sort by date.

1) Collection name – This is defined as the name of collection which was used with the sort method to sort the date field as per ascending or descending order.

2) Find – This method is used to display the result of the document which was sorted by the sort method in MongoDB. We can display the result of collection documents using the find method in MongoDB.

3) Sort – This method is used to sort the date field as per ascending or descending order. To sort the data of the collection field we need to define the name of the field with the sort method.

4) Name of date field – This parameter is defined as the name of date field on which we have applying the sort method to display the date collection result as per ascending or descending order.

5) 1 or -1 – This value is used with sort method to display the result as per ascending or descending order.

6) Aggregate – This method is used instead of the find method to sort the date type documents using the sort method in MongoDB. We can also use multiple fields with sort method to sort the result as per ascending or descending order.

How to sort by date in MongoDB?

We can sort the date in MongoDB by using sort method. We can sort the result as per ascending or descending order. To display the result using ascending order we need to use parameter value as 1, if suppose we need to display the result using descending order we need to use parameter value as -1. If suppose we have not used any value using date field or suppose we have defined any other value the query returns the error.

The below example shows that the query returns the error when we have not used any value or using different values except 1 and -1 with the sort method.

Code:

db.sort_date_test.find ().sort ( { date: 2 } )
db.sort_date_test.find ().sort ( { date: } )

mongoDB sort by date 1

Figure – query returns the error when we have not used any value or using different values except 1 and -1 with the sort method.

In the above first example, we have used 2 values with the sort method to display the result of a date field. But it will show the error message that bad sort specification because the valid value is only 1 or -1. In the second example, we have to pass empty values with the sort method to display the result of a date field. But it will show the syntax error message because we need to pass a valid value i.e. 1 or -1.

We can sort the data using the aggregation method, aggregation method is very useful to sort the date field in ascending or descending order. We can also use order by operator to sort the date type field documents in MongoDB. While using any operator we need to specify the value of ascending or descending order.

We can also sort the timestamp field using sort method in MongoDB. We can sort the timestamp field as per ascending or descending order. Using aggregation we can sort the timestamp and date, we can use multiple operators with the aggregation method to sort and display the result of collection documents.

Examples

Below is the example of sort by date in MongoDB.

1) Sort the date as per ascending order using the sort method

The below example shows sort the date as per ascending order using the sort method. We have used sort_date_test collection and date field to sort the documents. We have used value as 1 with date field to sort the date field as per ascending order.

Code:

db.sort_date_test.find ()
db.sort_date_test.find ().sort ( { date: 1 } )

mongoDB sort by date 2

Figure – Example of sort the date as per ascending order using sort method.

2) Sort the date as per descending order using sort method

The below example shows sort the date as per descending order using sort method. We have used sort_date_test collection and date field to sort the documents. We have used value as -1 with date field to sort the date field as per descending order.

Code:

db.sort_date_test.find ()
db.sort_date_test.find ().sort ( { date: -1 } )

mongoDB sort by date 3

Figure – Example of sort the date as per descending order using the sort method.

3) Sort the date as per ascending order by using the aggregate method

The below example shows sort the date as per ascending order using the aggregate method. We have used value as 1 with date field to sort the documents as per ascending order.

Code:

db.sort_date_test.find ()
db.sort_date_test.aggregate ( { $sort: { date: 1 } } )

mongoDB sort by date 4

Figure – Example of sort the date as per ascending order using the aggregate method.

4) Sort the date as per descending order by using the aggregate method

The below example shows sort the date as per descending order using the aggregate method. We have used value as -1 with date field to sort the documents as per descending order.

Code:

db.sort_date_test.find ()
db.sort_date_test.aggregate ( { $sort: { date: -1 } } )

output 1

Figure – Example of sort the date as per descending order using aggregate method.’

Conclusion

We can sort the date type field in MongoDB, for sorting date type documents we can use sort and aggregate method. To sort the date type documents as per ascending or descending order. If we need date in ascending order then we need to pass 1 with the sort method.

Recommended Articles

This is a guide to MongoDB sort by date. Here we discuss the definition, syntax, How to sort by date in MongoDB? Examples, and code implementation. You may also have a look at the following articles to learn more –

  1. MongoDB Timestamp
  2. MongoDB findOne()
  3. MongoDB group by
  4. MongoDB Database
C++ PROGRAMMING Certification Course
38+ Hours of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Certification Course
149+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL - Everything in SQL
253+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Certification Course
74+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MONGODB Certification Course
 29+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more