Spring Data MongoDB Date Between

后端 未结 9 2021
臣服心动
臣服心动 2020-12-23 17:47

I use spring data mongodb.

I want the records between two dates. The following MongoDB Query works:

db.posts.find({startDate: {$gte: start, $lt: end}         


        
9条回答
  •  没有蜡笔的小新
    2020-12-23 18:08

    You also can add query annotaion:

    @Query("{'date' : { $gte: ?0, $lte: ?1 } }")                 
    public List getObjectByDate(Date from, Date to); 
    

    Or proper spring data method signature:

    public List findByDateBetween(Date from, Date to);
    

    Both of these approaches give the same result. You can read more here https://www.baeldung.com/queries-in-spring-data-mongodb and here https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/

提交回复
热议问题