gpt4 book ai didi

java - 如何使用 Mongo Repository 查询同一个字段两次?

转载 作者:可可西里 更新时间:2023-11-01 10:37:34 24 4
gpt4 key购买 nike

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

最佳答案

您可以使用“之间”:

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

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

    @Query("{'date': {$gte: ?0, $lte:?1 }}")
List<TaskEntry> findByTask_DateBetween(Date dateFrom, Date dateTo);

关于java - 如何使用 Mongo Repository 查询同一个字段两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54202229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com