Spring Data MongoDB Date Between(Spring Data MongoDB 日期之间)
问题描述
我用的是spring data 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?
推荐答案
不要在您的标准中包含 'and("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));
当您包含 'and("startDate")' 部分时,Mongo 将其视为同一属性上的两个不同条目.
When you include the 'and("startDate")' part, Mongo see's it as two different entries on the same property.
这篇关于Spring Data MongoDB 日期之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring Data MongoDB 日期之间
				
        
 
            
        基础教程推荐
- 不推荐使用 Api 注释的描述 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - 从 python 访问 JVM 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				