Spring Data MongoDB Date Between

后端 未结 9 2010
臣服心动
臣服心动 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:18

    Do not include the 'and("startDate")' part in your criteria.

    Instead of :

    query.addCriteria(Criteria.where("startDate").gte(startDate).and("startDate").lt(endDate));
    

    You should use:

    query.addCriteria(Criteria.where("startDate").gte(startDate).lt(endDate));
    

    When you include the 'and("startDate")' part, Mongo see's it as two different entries on the same property.

提交回复
热议问题