Spring Mongo模板内部对象字段的投影

huangapple go评论74阅读模式
英文:

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:

{
  &quot;sourced&quot;: { &quot;reference&quot;: &quot;a&quot;, &quot;otherField: 1 }
  &quot;derived&quot;: &quot;Q&quot;
}

My final goal is to reduce it to:

{
  &quot;reference&quot;: &quot;a&quot;
}

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(&quot;sourced&quot;)
        );
AggregationResults&lt;ReducedEntity&gt; results = mongoTemplate.aggregate(aggregation, collectionName, ReducedEntity.class);
List&lt;ReducedEntity&gt; mappedResult = results.getMappedResults();
return mappedResult.stream().map(book -&gt; 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(&quot;sourced.reference&quot;)

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(&quot;sourced.reference&quot;), you no longer need the ReducedEntity class you directly use the Sourced class as outputType in the aggregate.

var aggregation = Aggregation.newAggregation(Aggregation.project(&quot;sourced.reference&quot;));

AggregationResults&lt;Sourced&gt; results = mongoTemplate.aggregate(aggregation, collectionName, Sourced.class);
return results.getMappedResults().stream().map(Sourced::getReference).collect(Collectors.toList());

huangapple
  • 本文由 发表于 2023年5月17日 18:27:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定