0

In my repository I've added the following method :

List<Event> findByEventDateBetween(LocalDate start, LocalDate end);

Generated query by spring-data-mongo is :

[debug] 2020-09-11 15:39:59,550 - o.s.d.m.c.MongoTemplate - find using query: { "eventDate" : { "$gt" : { "$date" : 1577833200000 }, "$lt" : { "$date" : 1599775200000 } } } fields: Document{{}} for class: class xxxxxx

Is there a way to tell spring data to use $gte and $lte instead of $gt and $lt when using Between keyword ?

flywell
  • 384
  • 3
  • 20

2 Answers2

2

You can take help of @Query annotation as follows:

@Query(value = "{'eventDate':{ $gte: ?0, $lte: ?1}}")
List<Event> findByEventDateBetween(LocalDate start, LocalDate end);
RLD
  • 1,867
  • 3
  • 15
  • 20
0

The following code is simpler:

List<Event> findByEventDateBetween(Range<LocalDate> dateRange);

call:

findByEventDateBetween(Range.closed(start, end))

reference:

https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repository-query-keywords

lower / upper bounds ($gt / $gte & $lt / $lte) according to Range
JianrongChen
  • 156
  • 1
  • 2