所以我想进行一个查询,查找我的集合中存在的所有任务,条件是在两个给定日期之间有一个日期属性。我使用了 Between 关键字,但它不包括结果中的参数值。
例如,如果参数为 2019-09-20 和 2019-09-25,则在响应中不会返回计划在相同日期执行的任务。
我尝试使用 LessThanEqual 和 GreaterThanEqual,但出现以下错误:
org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDocument, you can't add a second 'task.date' expression specified as 'task.date : Document{{$lte=Thu Sep 26 00:00:00 CEST 2019}}'. Criteria already contains 'task.date : Document{{$gte=Wed Sep 25 00:00:00 CEST 2019}}'.
这就是为什么我想知道是否有办法在同一个字段上查询两次。
这是我的存储库。
public interface TaskRepository extends MongoRepository<TaskEntry, String> {
List<TaskEntry> findByTask_DateGreaterThanEqualAndTask_DateLessThanEqual(Date dateFrom, Date dateTo);
}
谢谢。
更新:
这是使用 rustyx 的答案后我的集合、查询和结果的说明性示例。
这些是我的文件:
[
{
"id": "5c3da5b163c2789768d1402a",
"code": "0000001",
"createdDate": "2019-01-15T09:19:45.114+0000",
"lastModifiedDate": "2019-01-15T09:19:45.114+0000",
"task": {
"codeRule": null,
"title": "Task 1",
"date": "2019-09-26T00:00:00.000+0000",
"description": "This is the first task",
"type": "UNITARY",
"result": null
}
},
{
"id": "5c3da7dc63c2789768d1402b",
"code": "0000002",
"createdDate": "2019-01-15T09:29:00.125+0000",
"lastModifiedDate": "2019-01-15T09:29:00.125+0000",
"task": {
"codeRule": null,
"title": "Task 2",
"date": "2019-09-26T00:00:00.000+0000",
"description": "This is the second task",
"type": "UNITARY",
"result": null
}
},
{
"id": "5c3db0c763c27868b41e022b",
"code": "0000003",
"createdDate": "2019-01-15T10:07:03.693+0000",
"lastModifiedDate": "2019-01-15T10:07:03.693+0000",
"task": {
"codeRule": null,
"title": "Task 3",
"date": "2019-09-28T00:00:00.000+0000",
"description": "This is the third task",
"type": "UNITARY",
"result": null
}
}
]
这是我使用这个得到的响应:
@Query("{'task.date': {$gte: ?0, $lte:?1 }}")
List<TaskEntry> findByTask_DateBetween(Date dateFrom, Date dateTo);
和这个 :
/tasks?dateFrom=2019-09-26&dateTo=2019-09-28
回复 :
[
{
"id": "5c3da5b163c2789768d1402a",
"code": "0000001",
"createdDate": "2019-01-15T09:19:45.114+0000",
"lastModifiedDate": "2019-01-15T09:19:45.114+0000",
"task": {
"codeRule": null,
"title": "Task 1",
"date": "2019-09-26T00:00:00.000+0000",
"description": "This is the first task",
"type": "UNITARY",
"result": null
}
},
{
"id": "5c3da7dc63c2789768d1402b",
"code": "0000002",
"createdDate": "2019-01-15T09:29:00.125+0000",
"lastModifiedDate": "2019-01-15T09:29:00.125+0000",
"task": {
"codeRule": null,
"title": "Task 2",
"date": "2019-09-26T00:00:00.000+0000",
"description": "This is the second task",
"type": "UNITARY",
"result": null
}
}
]
日期为 2019-09-26 的任务被添加到响应中,这意味着$gte: ?0正在工作,但日期为 2019-09-28 的任务不存在。