spring-data-mongodb:从($gt,$lt)更改为($gte,$lte)

huangapple go评论128阅读模式
英文:

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

问题

在我的代码库中,我添加了以下方法:

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

spring-data-mongo 生成的查询如下:

[debug] 2020-09-11 15:39:59,550 - o.s.d.m.c.MongoTemplate - 使用查询:{ "eventDate" : { "$gt" : { "$date" : 1577833200000 }, "$lt" : { "$date" : 1599775200000 } } } 字段:Document{{}} 用于类:class xxxxxx

是否有办法告诉 spring data 在使用 Between 关键字时使用 $gte$lte 而不是 $gt$lt

英文:

In my repository I've added the following method :

List&lt;Event&gt; 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 ?

答案1

得分: 2

你可以按照以下方式使用 @Query 注解来获取帮助:

@Query(value = "{'eventDate': { $gte: ?0, $lte: ?1}}")
List<Event> findByEventDateBetween(LocalDate start, LocalDate end);
英文:

You can take help of @Query annotation as follows:

@Query(value = &quot;{&#39;eventDate&#39;:{ $gte: ?0, $lte: ?1}}&quot;)
List&lt;Event&gt; findByEventDateBetween(LocalDate start, LocalDate end);

答案2

得分: 0

以下是翻译好的内容:

下面的代码更简单:

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

调用:

findByEventDateBetween(Range.closed(start, end))

参考链接:

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

根据范围设置下限和上限($gt / $gte 和 $lt / $lte),参考链接中有解释。

英文:

The following code is simpler:

List&lt;Event&gt; findByEventDateBetween(Range&lt;LocalDate&gt; 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 &amp; $lt / $lte) according to Range

huangapple
  • 本文由 发表于 2020年9月11日 21:51:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63848428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定