How to select between dates Spring data MongoDB using @Query

人盡茶涼 提交于 2021-02-19 06:53:05

问题


I'm using Spring data with MongoDB and i need to find between actual day and 7 days ahead.

I have create repsoitories with @Query annotation and don't like to user Criteria class.

Do you have some idea how to user between with @Query?

thanks in advance.


回答1:


You can try below query.

Using @Query annotation

@Query(value = "{'date':{ $lt: ?0, $gt: ?1}}")
List<SomeClass> findByDateBetween(Instant from, Instant to);

Or

Using repository supported keywords

List<SomeClass> findByDateBetween(Instant from, Instant to);



回答2:


You can try thquery.

@Query(value = "{'champsDate':{ $gte: ?0, $lte: ?1}}")
List<entity> findByChampsDateBetween(Date startDate, Date endDate);


来源:https://stackoverflow.com/questions/43698985/how-to-select-between-dates-spring-data-mongodb-using-query

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