3

所以我想进行一个查询,查找我的集合中存在的所有任务,条件是在两个给定日期之间有一个日期属性。我使用了 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 的任务不存在。

4

1 回答 1

1

您可以使用“之间”:

    List<TaskEntry> findByTask_DateBetween(Date dateFrom, Date dateTo);

如果这还不够,请使用自定义查询:

    @Query("{'date': {$gte: ?0, $lte:?1 }}")
    List<TaskEntry> findByTask_DateBetween(Date dateFrom, Date dateTo);
于 2019-01-15T16:01:27.337 回答