I’ve a MongoDB collection that contains records with a field called createdAt, I want to get the last createdAt in the previous month of a given date using mongoTemplate aggregation in a spring boot application.
example of a record:
{
createdAt: new Date('2022-11-01'),
//other fields ...
}
Note:
Let say for 22/11/2022 given date, and we have records with createdAt fields are 12/10/2022, 22/10/2022, 27/10/2022; I wanna get the date : 27/10/2022
2
Answers
if you are using mongo template you could find it like this:
You can do that like this :
First create a date of first day of current month.
then use it in query: