Skip to content

Commit c738148

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2096 - Fix target field name for GraphLookup aggregation operation.
We now make sure to use the target field name instead of the alias when processing GraphLookupOperation. Original pull request: #613.
1 parent ec5268a commit c738148

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,16 @@ public DBObject toDBObject(AggregationOperationContext context) {
104104

105105
graphLookup.put("startWith", mappedStartWith.size() == 1 ? mappedStartWith.iterator().next() : mappedStartWith);
106106

107-
graphLookup.put("connectFromField", connectFrom.getName());
108-
graphLookup.put("connectToField", connectTo.getName());
107+
graphLookup.put("connectFromField", connectFrom.getTarget());
108+
graphLookup.put("connectToField", connectTo.getTarget());
109109
graphLookup.put("as", as.getName());
110110

111111
if (maxDepth != null) {
112112
graphLookup.put("maxDepth", maxDepth);
113113
}
114114

115115
if (depthField != null) {
116-
graphLookup.put("depthField", depthField.getName());
116+
graphLookup.put("depthField", depthField.getTarget());
117117
}
118118

119119
if (restrictSearchWithMatch != null) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperationUnitTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,40 @@ public void shouldRenderStartWithAggregationExpressions() {
133133
assertThat(dbObject, is(JSON.parse("{ $graphLookup : { from: \"employees\", startWith: { $literal: \"hello\"}, "
134134
+ "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }")));
135135
}
136+
137+
@Test // DATAMONGO-2096
138+
public void connectFromShouldUseTargetFieldInsteadOfAlias() {
139+
140+
AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId")
141+
.connectFrom("contacts.userId").connectTo("_id").depthField("numConnections").as("connections");
142+
143+
DBObject document = graphLookupOperation.toDBObject(Aggregation.DEFAULT_CONTEXT);
144+
145+
assertThat(document, is(JSON.parse(
146+
"{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"numConnections\" } }")));
147+
}
148+
149+
@Test // DATAMONGO-2096
150+
public void connectToShouldUseTargetFieldInsteadOfAlias() {
151+
152+
AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId")
153+
.connectFrom("userId").connectTo("connectto.field").depthField("numConnections").as("connections");
154+
155+
DBObject document = graphLookupOperation.toDBObject(Aggregation.DEFAULT_CONTEXT);
156+
157+
assertThat(document, is(JSON.parse(
158+
"{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"userId\", \"connectToField\" : \"connectto.field\", \"as\" : \"connections\", \"depthField\" : \"numConnections\" } }")));
159+
}
160+
161+
@Test // DATAMONGO-2096
162+
public void depthFieldShouldUseTargetFieldInsteadOfAlias() {
163+
164+
AggregationOperation graphLookupOperation = Aggregation.graphLookup("user").startWith("contacts.userId")
165+
.connectFrom("contacts.userId").connectTo("_id").depthField("foo.bar").as("connections");
166+
167+
DBObject document = graphLookupOperation.toDBObject(Aggregation.DEFAULT_CONTEXT);
168+
169+
assertThat(document, is(JSON.parse(
170+
"{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"foo.bar\" } }")));
171+
}
136172
}

0 commit comments

Comments
 (0)