Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JPA get dates between a range (inclusive) in mongo with LocalDateTime

Very similar to this question: Check date between two other dates spring data jpa

However, I am attempting to do this with MongoDB and java.time.LocalDateTime.

I have tried:

  • findAllByMetadataStartTimeBetween(start, end) (works, but exclusive start/end)
  • findAllByMetadataStartTimeGreaterThanEqual(start) (works, but no end)

However, when I try: findAllByMetadataStartTimeGreaterThanEqualAndMetadataStartTimeLessThanEqual(start,end)

I get the error: json can't serialize type : class java.time.LocalDateTime

Any idea why this particular combination throws this error, when the previous ones do not?

like image 577
pennstatephil Avatar asked Oct 16 '25 20:10

pennstatephil


1 Answers

Try the below code. I am sure, works for inclusive

@Query("{'dateTime' : { $gte: ?0, $lte: ?1 } }")
List<YourObject> findByDateTimeBetween(Date from, Date to);

Spring data method signature below works for exclusive

public List<YourObject> findByDateBetween(Date from, Date to);
like image 157
Mallikarjun Vishwanath Avatar answered Oct 19 '25 11:10

Mallikarjun Vishwanath