How compare date in spring mongo


How compare date in spring mongo



I want to fetch result bw two date in Mongo and spring but it show parse exception.



If I pass like date as string then no result. How compare date in spring and Mongo application.


Aggregation.match(Criteria.where("date").gte(new Date("2018-06-24")).lte(new Date("2018-06-30")));



My collection:


{
"_id" : ObjectId("5b34a31a68f1b041aa13b82f"),
"date" : ISODate("2018-06-28T00:00:00Z"),
"eventname" : "app open"
}




1 Answer
1



You can try something like:


DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = df.parse("2018-06-24");
Date endDate = df.parse("2018-06-30");
Aggregation.match(Criteria.where("date").gte(startDate).lte(endDate));



Please see also Spring Data MongoDB Date Between and Spring data mongodb search for ISO date for further information.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy