gpt4 book ai didi

annotations - 自定义 Spring Data @Query 中的 MongoDB 聚合

转载 作者:行者123 更新时间:2023-12-04 03:02:56 26 4
gpt4 key购买 nike

任何人都知道如何将 MongoDB 聚合查询硬塞到 spring-data 自定义 @Query 中?

我想对我的实体进行分组并按日期检索所有最新版本。

聚合查询是

db.collection.aggregate(
[
{ $sort: { url: 1, checkedOn: 1 } },
{
$group: {
_id: {url: "$url", path: "$path" },
lastChecked: { $last: "$checkedOn" }
}
}
])

这运行良好并返回如下集合:
{
"result" : [
{
"_id" : {
"url" : "http://www.google.co.uk",
"path" : "/about"
},
"lastChecked" : ISODate("2015-03-17T11:43:01.222Z")
},
{
"_id" : {
"url" : "http://www.google.co.uk",
"path" : "/about2"
},
"lastChecked" : ISODate("2015-03-17T11:43:04.213Z")
}
],
"ok" : 1
}

我想将其作为 spring-data 自定义查询的一部分来执行:
@RepositoryRestResource(collectionResourceRel = "healthcheck", path = "healthcheck")
public interface LinkRepository extends MongoRepository<Link, String> {

@Query("{ 'status' : { $gt: 299 }, 'path' : ?0 }" )
List<CheckedLink> findByPathAndInvalid(@Param("path") String path);

@Query(**aggregate query goes here**)
List<CheckedLink> findByLatest();

}

我无法将聚合查询直接放入 @Query 中,因为生成的集合没有直接映射到 Link 对象。

我能找到的唯一引用是一个旧的 Spring-Sandbox 项目,但自 2013 年以来一直没有更新:
https://github.com/hantsy/spring-sandbox/wiki/spring-data-mongo#custom-query

最佳答案

@Query注解只能用于填充 mongo 请求的查询部分。由于其他注释类是 CountQuery , DeleteQueryExistsQuery ,我认为使用注释是不可能的。

关于annotations - 自定义 Spring Data @Query 中的 MongoDB 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29106731/

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