Skip to content
Advertisement

spring-data-mongodb : Between change from ($gt, $lt) to ($gte, $lte)

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 ?

Advertisement

Answer

You can take help of @Query annotation as follows:

@Query(value = "{'eventDate':{ $gte: ?0, $lte: ?1}}")
List<Event> findByEventDateBetween(LocalDate start, LocalDate end);
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement