Skip to content

Commit e09dcfb

Browse files
authored
Merge pull request #251 from jmarkerink/fix/dot-notation-projection-to-nested-object
fix: dot notation projection to nested object
2 parents 72f4959 + 3741fc7 commit e09dcfb

2 files changed

Lines changed: 81 additions & 3 deletions

File tree

core/src/main/java/de/bwaldvogel/mongo/backend/aggregation/stage/ProjectStage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ Document projectDocument(Document document) {
7575
.stream()
7676
.map(value -> Expression.evaluateDocument(value, document))
7777
.collect(Collectors.toList());
78-
result.put(field, resolvedProjectionValues);
78+
Utils.changeSubdocumentValue(result, field, resolvedProjectionValues);
7979
} else if (projectionValue == null) {
80-
result.put(field, null);
80+
Utils.changeSubdocumentValue(result, field, null);
8181
} else {
8282
Object value = Expression.evaluateDocument(projectionValue, document);
8383
if (!(value instanceof Missing)) {
84-
result.put(field, value);
84+
Utils.changeSubdocumentValue(result, field, value);
8585
}
8686
}
8787
}

test-common/src/main/java/de/bwaldvogel/mongo/backend/AbstractAggregationTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,84 @@ void testAggregateWithRedact() {
27142714
.containsOnly(json("_id: 1"));
27152715
}
27162716

2717+
@Test
2718+
void dotNotationProjection() {
2719+
collection.insertOne(json("""
2720+
_id: 1,
2721+
companyName: 'Walt Disney',
2722+
buildingNumber: 500,
2723+
streetName: 'South Buena Vista Street',
2724+
city: 'Burbank',
2725+
state: 'California',
2726+
zipCode: 91502
2727+
"""));
2728+
2729+
assertThat(collection.aggregate(jsonList("""
2730+
$project: {
2731+
Name: "$companyName",
2732+
"Address.City": "$city",
2733+
"Address.ZipCode": "$zipCode",
2734+
"Address.StreetName": "$streetName",
2735+
"Address.BuildingNumber": "$buildingNumber",
2736+
}
2737+
""")))
2738+
.containsExactly(json("""
2739+
_id: 1,
2740+
Name: 'Walt Disney',
2741+
Address: {
2742+
City: 'Burbank',
2743+
ZipCode: 91502,
2744+
StreetName: 'South Buena Vista Street',
2745+
BuildingNumber: 500
2746+
}
2747+
"""));
2748+
}
2749+
2750+
@Test
2751+
void dotNotationProjectionDeepNesting() {
2752+
collection.insertOne(json("""
2753+
_id: 1,
2754+
companyName: 'Acme Corp',
2755+
country: 'USA',
2756+
state: 'California',
2757+
city: 'San Francisco',
2758+
street: 'Market Street',
2759+
buildingNumber: 123,
2760+
floor: 5,
2761+
unit: 'A'
2762+
"""));
2763+
2764+
assertThat(collection.aggregate(jsonList("""
2765+
$project: {
2766+
Company: "$companyName",
2767+
"Location.Country": "$country",
2768+
"Location.Address.State": "$state",
2769+
"Location.Address.City": "$city",
2770+
"Location.Address.Details.Street": "$street",
2771+
"Location.Address.Details.BuildingNumber": "$buildingNumber",
2772+
"Location.Address.Details.Floor": "$floor",
2773+
"Location.Address.Details.Unit": "$unit"
2774+
}
2775+
""")))
2776+
.containsExactly(json("""
2777+
_id: 1,
2778+
Company: 'Acme Corp',
2779+
Location: {
2780+
Country: 'USA',
2781+
Address: {
2782+
State: 'California',
2783+
City: 'San Francisco',
2784+
Details: {
2785+
Street: 'Market Street',
2786+
BuildingNumber: 123,
2787+
Floor: 5,
2788+
Unit: 'A'
2789+
}
2790+
}
2791+
}
2792+
"""));
2793+
}
2794+
27172795
// https://github.com/bwaldvogel/mongo-java-server/issues/191
27182796
@Test
27192797
void testProjectWithCondition() throws Exception {

0 commit comments

Comments
 (0)