Java – Spring JPA uses LocalDateTime to get dates between ranges (inclusive) in mongo

Spring JPA uses LocalDateTime to get dates between ranges (inclusive) in mongo… here is a solution to the problem.

Spring JPA uses LocalDateTime to get dates between ranges (inclusive) in mongo

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

However, I’m trying to do this using MongoDB and java.time.LocalDateTime.

I tried:

  • findAllByMetadataStartTimeBetween(start, end) (valid, but not start/end).
  • findAllByMetadataStartTimeGreaterThanEqual(start)

But when I try: findAllByMetadataStartTimeGreaterThanEqualAndMetadataStartTimeLessThanEqual(start, end).

I get the error: json Unable to serialize type: class java.time.LocalDateTime

Any idea why this particular combination throws this error when the previous combination doesn’t?

Solution

Try the code below. I’m sure it works for inclusion

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

The following Spring Data method signatures are proprietary

public List<YourObject> findByDateBetween(Date from, Date to);

Related Problems and Solutions