diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java index 079815681..de77ecdee 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java @@ -104,8 +104,8 @@ public class GraphLookupOperation implements InheritsFieldsAggregationOperation graphLookup.put("startWith", mappedStartWith.size() == 1 ? mappedStartWith.iterator().next() : mappedStartWith); - graphLookup.put("connectFromField", connectFrom.getName()); - graphLookup.put("connectToField", connectTo.getName()); + graphLookup.put("connectFromField", connectFrom.getTarget()); + graphLookup.put("connectToField", connectTo.getTarget()); graphLookup.put("as", as.getName()); if (maxDepth != null) { @@ -113,7 +113,7 @@ public class GraphLookupOperation implements InheritsFieldsAggregationOperation } if (depthField != null) { - graphLookup.put("depthField", depthField.getName()); + graphLookup.put("depthField", depthField.getTarget()); } if (restrictSearchWithMatch != null) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java index f4e91860f..d68404015 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java @@ -133,4 +133,40 @@ public class GraphLookupOperationUnitTests { assertThat(dbObject, is(JSON.parse("{ $graphLookup : { from: \"employees\", startWith: { $literal: \"hello\"}, " + "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }"))); } + + @Test // DATAMONGO-2096 + public void connectFromShouldUseTargetFieldInsteadOfAlias() { + + AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId") + .connectFrom("contacts.userId").connectTo("_id").depthField("numConnections").as("connections"); + + DBObject document = graphLookupOperation.toDBObject(Aggregation.DEFAULT_CONTEXT); + + assertThat(document, is(JSON.parse( + "{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"numConnections\" } }"))); + } + + @Test // DATAMONGO-2096 + public void connectToShouldUseTargetFieldInsteadOfAlias() { + + AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId") + .connectFrom("userId").connectTo("connectto.field").depthField("numConnections").as("connections"); + + DBObject document = graphLookupOperation.toDBObject(Aggregation.DEFAULT_CONTEXT); + + assertThat(document, is(JSON.parse( + "{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"userId\", \"connectToField\" : \"connectto.field\", \"as\" : \"connections\", \"depthField\" : \"numConnections\" } }"))); + } + + @Test // DATAMONGO-2096 + public void depthFieldShouldUseTargetFieldInsteadOfAlias() { + + AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId") + .connectFrom("contacts.userId").connectTo("_id").depthField("foo.bar").as("connections"); + + DBObject document = graphLookupOperation.toDBObject(Aggregation.DEFAULT_CONTEXT); + + assertThat(document, is(JSON.parse( + "{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"foo.bar\" } }"))); + } }