我已编写此代码以建立以下条件:
Criteria filterCriteria = Criteria.where("application").is(applicationId);
if (null != from) {
filterCriteria = filterCriteria
.and("timestamp").gte(from);
}
if (null != to) {
filterCriteria = filterCriteria
.and("timestamp").lte(to);
}
我收到此异常消息:
由于com.mongodb.BasicDocument的限制,您不能添加第二个指定为“ timestamp:Document {{$ lte = Sat Oct 10 00:00:00 CEST 2020}}}的'timestamp'表达式。条件已经包含“时间戳记:文档{{$$ gte = WEST Oct 10 00:00:00 CEST 2018}}}”。
有什么想法吗?
答案 0 :(得分:0)
您必须在单个gte
操作上同时添加lte
和and
。这就是我正在做一个非常类似的查询的方式:
if (from != null && to != null) {
criteria = criteria.and("timestamp").gte(from).lte(to)
}
else if (from != null) {
criteria = criteria.and("timestamp").gte(from)
}
else if (to != null) {
criteria = criteria.and("timestamp").lte(to)
}