英文:
Spring mongo template projection of inner object field
问题
Sure, here's the translated code snippet:
我的对象看起来是这样的:
{
"sourced": { "reference": "a", "otherField": 1 },
"derived": "Q"
}
我的最终目标是将其减少为:
{
"reference": "a"
}
并使用distinct删除所有重复项。
到目前为止,我得到了以下工作代码:
@Data
private static class ReducedEntity {
Sourced sourced;
}
@Data
private static class Sourced {
private String reference;
}
var aggregation = Aggregation.newAggregation(
match(new Criteria()),
Aggregation.project("sourced")
);
AggregationResults<ReducedEntity> results = mongoTemplate.aggregate(aggregation, collectionName, ReducedEntity.class);
List<ReducedEntity> mappedResult = results.getMappedResults();
return mappedResult.stream().map(book -> book.sourced.reference).collect(Collectors.toList());
它可以工作,但我更喜欢拥有一个具有reference属性的单一类。
一个有趣的观察是
Aggregation.project("sourced.reference")
将返回`sourced`的null值。
我尝试将"sourced.reference"投影到"reference",但没有成功。问题是什么?谢谢
Please note that I've translated the code and removed the part where you asked not to answer the translation question. If you need further assistance or have specific questions about the code, feel free to ask.
英文:
My object looks this way:
{
"sourced": { "reference": "a", "otherField: 1 }
"derived": "Q"
}
My final goal is to reduce it to:
{
"reference": "a"
}
And remove all duplicates with distinct.
So far I got following working code:
@Data
private static class ReducedEntity {
Sourced sourced;
}
@Data
private static class Sourced {
private String reference;
}
var aggregation = Aggregation.newAggregation(
match(new Criteria()),
Aggregation.project("sourced")
);
AggregationResults<ReducedEntity> results = mongoTemplate.aggregate(aggregation, collectionName, ReducedEntity.class);
List<ReducedEntity> mappedResult = results.getMappedResults();
return mappedResult.stream().map(book -> book.sourced.reference).collect(Collectors.toList());
It works but I prefer to have a single class with a reference property.
One interesting observation is that
Aggregation.project("sourced.reference")
will return null for sourced
.
I tried to project "sourced.reference" to "reference" without luck. What's the issue? Thanks
答案1
得分: 1
使用Aggregation.project("sourced.reference")
,您不再需要ReducedEntity
类,您可以直接将Sourced
类用作聚合的输出类型。
var aggregation = Aggregation.newAggregation(Aggregation.project("sourced.reference"));
AggregationResults<Sourced> results = mongoTemplate.aggregate(aggregation, collectionName, Sourced.class);
return results.getMappedResults().stream().map(Sourced::getReference).collect(Collectors.toList());
如果您需要进一步的帮助,请告诉我。
英文:
With Aggregation.project("sourced.reference")
, you no longer need the ReducedEntity
class you directly use the Sourced
class as outputType in the aggregate.
var aggregation = Aggregation.newAggregation(Aggregation.project("sourced.reference"));
AggregationResults<Sourced> results = mongoTemplate.aggregate(aggregation, collectionName, Sourced.class);
return results.getMappedResults().stream().map(Sourced::getReference).collect(Collectors.toList());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论