我用的是spring data mongodb。
我想要两个日期之间的记录。以下 MongoDB 查询有效:
db.posts.find({startDate: {$gte: start, $lt: end}});
我尝试的 Spring 数据查询对象代码翻译不起作用:
Query query = new Query();
query.addCriteria(Criteria.where("startDate").gte(startDate)
.and("startDate").lt(endDate));
构建我需要的 Mongo 查询的方法调用的正确顺序是什么?
最佳答案
不要在您的条件中包含 'and("startDate")' 部分。
而不是:
query.addCriteria(Criteria.where("startDate").gte(startDate).and("startDate").lt(endDate));
你应该使用:
query.addCriteria(Criteria.where("startDate").gte(startDate).lt(endDate));
当您包含 'and("startDate")' 部分时,Mongo 将其视为同一属性上的两个不同条目。
关于java - Spring Data MongoDB 日期之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10311061/