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.
This commit is contained in:
Christoph Strobl
2018-10-01 14:31:59 +02:00
committed by Mark Paluch
parent 7f9c1bd774
commit ab568229b5
2 changed files with 42 additions and 9 deletions

View File

@@ -103,8 +103,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) {
@@ -112,7 +112,7 @@ public class GraphLookupOperation implements InheritsFieldsAggregationOperation
}
if (depthField != null) {
graphLookup.put("depthField", depthField.getName());
graphLookup.put("depthField", depthField.getTarget());
}
if (restrictSearchWithMatch != null) {

View File

@@ -24,10 +24,6 @@ import org.junit.Test;
import org.springframework.data.mongodb.core.Person;
import org.springframework.data.mongodb.core.query.Criteria;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;
/**
* Unit tests for {@link GraphLookupOperation}.
*
@@ -104,8 +100,9 @@ public class GraphLookupOperationUnitTests {
Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(document,
is(Document.parse("{ $graphLookup : { from: \"employees\", startWith: [\"$reportsTo\", { $literal: \"$boss\"}], "
+ "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }")));
is(Document
.parse("{ $graphLookup : { from: \"employees\", startWith: [\"$reportsTo\", { $literal: \"$boss\"}], "
+ "connectFromField: \"reportsTo\", connectToField: \"name\", as: \"reportingHierarchy\" } }")));
}
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1551
@@ -134,4 +131,40 @@ public class GraphLookupOperationUnitTests {
assertThat(document, is(Document.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");
Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(document, is(Document.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");
Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(document, is(Document.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");
Document document = graphLookupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
assertThat(document, is(Document.parse(
"{ \"$graphLookup\" : { \"from\" : \"user\", \"startWith\" : \"$contacts.userId\", \"connectFromField\" : \"contacts.userId\", \"connectToField\" : \"_id\", \"as\" : \"connections\", \"depthField\" : \"foo.bar\" } }")));
}
}