Spring Data MongoDB之间的日期 [英] Spring Data MongoDB Date Between
本文介绍了Spring Data MongoDB之间的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用spring数据mongodb。
I use spring data mongodb.
我想要两个日期之间的记录。以下MongoDB查询有效:
I want the records between two dates. The following MongoDB Query works:
db.posts.find({startDate: {$gte: start, $lt: end}});
我尝试的Spring数据查询对象代码转换不起作用:
My attempted Spring data query object code translation does not work:
Query query = new Query();
query.addCriteria(Criteria.where("startDate").gte(startDate)
.and("startDate").lt(endDate));
构建我需要的Mongo查询的方法调用的正确顺序是什么?
What is the correct order of method calls to build the Mongo query I need?
推荐答案
请勿在条件中包含和(startDate)'部分。
Do not include the 'and("startDate")' part in your criteria.
而不是:
query.addCriteria(Criteria.where("startDate").gte(startDate).and("startDate").lt(endDate));
你应该使用:
query.addCriteria(Criteria.where("startDate").gte(startDate).lt(endDate));
当您包含'和(startDate)'部分时,Mongo将其视为两个不同的条目在同一财产。
When you include the 'and("startDate")' part, Mongo see's it as two different entries on the same property.
这篇关于Spring Data MongoDB之间的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文