Skip to content

Commit bab1198

Browse files
committed
DATAMONGO-2637 - Fields and projection API are very inconvenient
1 parent 53fcdb7 commit bab1198

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,9 @@ public AggregationField(String name, @Nullable String target) {
233233
String targetToSet = target != null ? cleanUp(target) : null;
234234

235235
Assert.hasText(nameToSet, "AggregationField name must not be null or empty!");
236-
237-
if (target == null && name.contains(".")) {
238-
this.name = nameToSet.substring(nameToSet.indexOf('.') + 1);
239-
this.target = nameToSet;
240-
} else {
241-
this.name = nameToSet;
242-
this.target = targetToSet;
243-
}
236+
237+
this.name = nameToSet;
238+
this.target = target == null ? nameToSet : targetToSet;
244239
}
245240

246241
private static String cleanUp(String source) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,6 +2255,34 @@ void nestedMappedFieldReferenceInArrayField() {
22552255
assertThat(doc).isEqualTo(Document.parse(
22562256
"{ $project: { \"author\" : 1, \"myArray\" : [ \"$ti_t_le\", \"plain - string\", { \"$sum\" : [\"$ti_t_le\", 10] } ] } } ] }"));
22572257
}
2258+
2259+
@Test
2260+
void withNestedFieldsIncludeProjection(){
2261+
2262+
Document doc = new ProjectionOperation()
2263+
.andInclude("foo")
2264+
.andInclude("foo.bar")
2265+
.andInclude("foo.bar.baz")
2266+
.toDocument(Aggregation.DEFAULT_CONTEXT);
2267+
2268+
assertThat(doc).isEqualTo(Document.parse(
2269+
"{\"$project\" : {\"foo\" : 1, \"foo.bar\" : 1, \"foo.bar.baz\" : 1}}"));
2270+
2271+
}
2272+
2273+
@Test
2274+
void withNestedFieldsExcludeProjection(){
2275+
2276+
Document doc = new ProjectionOperation()
2277+
.andExclude("foo")
2278+
.andExclude("foo.bar")
2279+
.andExclude("foo.bar.baz")
2280+
.toDocument(Aggregation.DEFAULT_CONTEXT);
2281+
2282+
assertThat(doc).isEqualTo(Document.parse(
2283+
"{\"$project\" : {\"foo\" : 0, \"foo.bar\" : 0, \"foo.bar.baz\" : 0}}"));
2284+
2285+
}
22582286

22592287
private static Document extractOperation(String field, Document fromProjectClause) {
22602288
return (Document) fromProjectClause.get(field);

0 commit comments

Comments
 (0)