Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

java - Spring Data Aggregation query with nested projection

I'm having troubles to map a pure json query to it's spring data representation:

db.getCollection('taxon').aggregate( 
  [
    { $match: { "value.id": "someId123" } },
    {
      $graphLookup: {
        from: "taxon",
        startWith: "$value.id",
        connectFromField: "value.id",
        connectToField: "value.parentId",
        as: "children"}
    },
    { $project : { "children.value.unterwarengruppenIds" : 1 } },
    { $unwind: "$children"},
    { $unwind: "$children.value.unterwarengruppenIds"},
    { $replaceRoot: { newRoot: "$children.value" } } 
  ] 
)

I've tried this but apparently, my projection does not work:

Aggregation aggregation = Aggregation.newAggregation(
  Aggregation.match(Criteria.where("value.id").is(taxon.getId().getValue())),
  Aggregation.graphLookup(COLLECTION_NAME)
    .startWith("$value.id")
    .connectFrom("value.id")
    .connectTo("value.parentId")
    .as("children"),
  Aggregation.project().<NOT SURE HOW TO THIS HERE>,
  Aggregation.unwind("children"),
  Aggregation.unwind("children.value.unterwarengruppenIds"),
  Aggregation.replaceRoot("children.value")
}.withOptions((Aggregation.newAggregationOptions().allowDiskUse(true)
.build()));

I did spend some time but could not figure out how to simply project a nested field. So can I map this $project : { "children.value.unterwarengruppenIds" : 1 } to a spring data representation? Any help is appreciated :)

question from:https://stackoverflow.com/questions/65952112/spring-data-aggregation-query-with-nested-projection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...