嵌套 class object 与 Spring 数据 MongoDb 聚合 returns 空字段
Nested class object with Spring data MongoDb aggregation returns null field
我对在聚合中包含嵌套 class 有疑问。
这是我 collection 中 json 文档的预览:
{
"id": "1234",
"typeApp": "API",
"name": "name",
"versionNum": "1",
"release": {
"author": "name",
//some other data
}
}
文档javaclass:
@Document(collection = "myClassExamples")
public class MyClassExampleDocument {
@Id
private String id;
private String typeApp;
private String name;
private String versionNum;
private Release release;
public static class Release {
private String author;
//Other fields...
}
}
我正在尝试构建一个查询,以在参数中按给定的 typeApp 查找最后一个文档组,并按 versionNum DESC[=41= 排序] 通过 typeApp.
获取新的
我从一个更简单的查询开始,一个简单的分组 typeApp :
Aggregation aggregation = newAggregation(
Aggregation.sort(Sort.Direction.DESC, "versionNum"),
Aggregation.group("typeApp"),
project(MyClassExampleDocument.class)
)
查询 returns MyClassExampleDocument 的列表,所有字段都具有空值,除了 id 已填充使用 typeApp.
您知道如何构建聚合以获得存储在我的 collection 中的整个 object 吗?
感谢您的帮助!
您可以像下面这样使用
public List<MyClassExampleDocument> test() {
Aggregation aggregation = Aggregation.newAggregation(
sort(Sort.Direction.DESC, "versionNum"),
group("typeApp").first("$$ROOT").as("data")
replaceRoot("data")
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(MyClassExampleDocument.class), MyClassExampleDocument.class).getMappedResults();
}
这里是聚合
db.collection.aggregate([
{ "$sort": { versionNum: -1 } },
{
"$group": {
"_id": "$typeApp",
"data": { "$first": "$$ROOT" }
}
},
{ "$replaceRoot": { "newRoot": "$data" } }
])
注意:java 代码未经过测试,它是基于工作 mongo 脚本
实现的
我对在聚合中包含嵌套 class 有疑问。
这是我 collection 中 json 文档的预览:
{
"id": "1234",
"typeApp": "API",
"name": "name",
"versionNum": "1",
"release": {
"author": "name",
//some other data
}
}
文档javaclass:
@Document(collection = "myClassExamples")
public class MyClassExampleDocument {
@Id
private String id;
private String typeApp;
private String name;
private String versionNum;
private Release release;
public static class Release {
private String author;
//Other fields...
}
}
我正在尝试构建一个查询,以在参数中按给定的 typeApp 查找最后一个文档组,并按 versionNum DESC[=41= 排序] 通过 typeApp.
获取新的我从一个更简单的查询开始,一个简单的分组 typeApp :
Aggregation aggregation = newAggregation(
Aggregation.sort(Sort.Direction.DESC, "versionNum"),
Aggregation.group("typeApp"),
project(MyClassExampleDocument.class)
)
查询 returns MyClassExampleDocument 的列表,所有字段都具有空值,除了 id 已填充使用 typeApp.
您知道如何构建聚合以获得存储在我的 collection 中的整个 object 吗?
感谢您的帮助!
您可以像下面这样使用
public List<MyClassExampleDocument> test() {
Aggregation aggregation = Aggregation.newAggregation(
sort(Sort.Direction.DESC, "versionNum"),
group("typeApp").first("$$ROOT").as("data")
replaceRoot("data")
).withOptions(AggregationOptions.builder().allowDiskUse(Boolean.TRUE).build());
return mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(MyClassExampleDocument.class), MyClassExampleDocument.class).getMappedResults();
}
这里是聚合
db.collection.aggregate([
{ "$sort": { versionNum: -1 } },
{
"$group": {
"_id": "$typeApp",
"data": { "$first": "$$ROOT" }
}
},
{ "$replaceRoot": { "newRoot": "$data" } }
])
注意:java 代码未经过测试,它是基于工作 mongo 脚本
实现的