spring 数据 mongodb: graphlookup 查询不包含 as-field

spring data mongodb: graphlookup query does not contain as-field

我正在尝试在 spring 数据 mongoDB:

中实施此查询
db.myCollection.aggregate(
    [ 
        { "$graphLookup": { 
            "from": "myCollection", 
            "startWith": "$name", 
            "connectFromField": "name", 
            "connectToField": "parent", 
            "as": "descendants"
        }},
        { "$match": { "name": "someValue" } }, 
    ]
)

   GraphLookupOperation graphLookupOperation = GraphLookupOperation.builder()                    .from(mongoOperations.getCollectionName(MyCollection.class))
                    .startWith("name")
                    .connectFrom("name")
                    .connectTo("parent")
                    .as("descendants");
            MatchOperation matchStage = Aggregation.match(new Criteria("name").is("someValue));
            Aggregation aggregation = Aggregation.newAggregation(graphLookupOperation, matchStage);
            List<ClassDefinitionDocument> results = mongoOperations.aggregate(aggregation, mongoOperations.getCollectionName(MyCollection.class), MyCollection.class).getMappedResults();

但是,results 列表不包含 descendants 字段。 javascript 查询在 mongo shell.

中完美运行

我做错了什么?

我开始工作了。你可以试试这个让它为你工作。

A TypedAggregation is a special Aggregation that holds information of the input aggregation type.

public void graphLookupShouldBeAppliedCorrectly() {

    assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_FOUR));

    Employee em1 = Employee.builder().id(1).name("Dev").build();
    Employee em2 = Employee.builder().id(2).name("Eliot").reportsTo("Dev").build();
    Employee em4 = Employee.builder().id(4).name("Andrew").reportsTo("Eliot").build();

    mongoTemplate.insert(Arrays.asList(em1, em2, em4), Employee.class);

    TypedAggregation<Employee> agg = Aggregation.newAggregation(Employee.class,
            match(Criteria.where("name").is("Andrew")), //
            Aggregation.graphLookup("employee") //
                    .startWith("reportsTo") //
                    .connectFrom("reportsTo") //
                    .connectTo("name") //
                    .depthField("depth") //
                    .maxDepth(5) //
                    .as("reportingHierarchy"));

    AggregationResults<Document> result = mongoTemplate.aggregate(agg, Document.class);

    Document object = result.getUniqueMappedResult();
    List<Object> list = (List<Object>) object.get("reportingHierarchy");

    assertThat(object, isBsonObject().containing("reportingHierarchy", List.class));
    assertThat((Document) list.get(0), isBsonObject().containing("name", "Dev").containing("depth", 1L));
    assertThat((Document) list.get(1), isBsonObject().containing("name", "Eliot").containing("depth", 0L));
}