MongoRepository query for between dates

断了今生、忘了曾经 提交于 2020-12-08 06:28:14

问题


My 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();
}

I plan to use like following.

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

Is there a way i can put the following query to the above query annotation and how i can do greater than and less than logic

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

回答1:


You can try following couple of ways.

Without Query Annotation.

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

With Query Annotation.

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


来源:https://stackoverflow.com/questions/42189770/mongorepository-query-for-between-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!