2017-02-12 85 views
2

之间MongoRepository查询日期

我的POJO

public class PacketData implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    private final String token = UUID.randomUUID().toString(); 

    private final ZonedDateTime arrived = ZonedDateTime.now(); 
} 
我打算使用像以下。

@Query("?") 
List<PacketData> findPacketArrivedBetween(ZonedDateTime startDate, ZonedDateTime endDate); 

有没有一种方法,我可以把下面的查询上述query注释,以及如何我可以做的比且小于逻辑

Query query = new Query().addCriteria(Criteria.where("arrived").gte(startDate).lte(endDate)); 
+0

Spring数据MongoDB没有内置的'ZonedDateTime'支持,只有'LocalDateTime'。使用'ZonedDateTime'需要一个[自定义转换器](http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mapping-explicit-converters)来保存时区。 – mp911de

回答

2

你可以尝试以下几种方法。

没有查询注释。

List<PacketData> findByArrivedBetween(ZonedDateTime startDate, ZonedDateTime endDate); 

使用查询注释。

@Query("{'arrived': {$gte: ?0, $lte:?1 }}") 
List<PacketData> findPacketArrivedBetween(ZonedDateTime startDate, ZonedDateTime endDate);