Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ Document projectDocument(Document document) {
.stream()
.map(value -> Expression.evaluateDocument(value, document))
.collect(Collectors.toList());
result.put(field, resolvedProjectionValues);
Utils.changeSubdocumentValue(result, field, resolvedProjectionValues);
} else if (projectionValue == null) {
result.put(field, null);
Utils.changeSubdocumentValue(result, field, null);
} else {
Object value = Expression.evaluateDocument(projectionValue, document);
if (!(value instanceof Missing)) {
result.put(field, value);
Utils.changeSubdocumentValue(result, field, value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2714,6 +2714,84 @@ void testAggregateWithRedact() {
.containsOnly(json("_id: 1"));
}

@Test
void dotNotationProjection() {
collection.insertOne(json("""
_id: 1,
companyName: 'Walt Disney',
buildingNumber: 500,
streetName: 'South Buena Vista Street',
city: 'Burbank',
state: 'California',
zipCode: 91502
"""));

assertThat(collection.aggregate(jsonList("""
$project: {
Name: "$companyName",
"Address.City": "$city",
"Address.ZipCode": "$zipCode",
"Address.StreetName": "$streetName",
"Address.BuildingNumber": "$buildingNumber",
}
""")))
.containsExactly(json("""
_id: 1,
Name: 'Walt Disney',
Address: {
City: 'Burbank',
ZipCode: 91502,
StreetName: 'South Buena Vista Street',
BuildingNumber: 500
}
"""));
}

@Test
void dotNotationProjectionDeepNesting() {
collection.insertOne(json("""
_id: 1,
companyName: 'Acme Corp',
country: 'USA',
state: 'California',
city: 'San Francisco',
street: 'Market Street',
buildingNumber: 123,
floor: 5,
unit: 'A'
"""));

assertThat(collection.aggregate(jsonList("""
$project: {
Company: "$companyName",
"Location.Country": "$country",
"Location.Address.State": "$state",
"Location.Address.City": "$city",
"Location.Address.Details.Street": "$street",
"Location.Address.Details.BuildingNumber": "$buildingNumber",
"Location.Address.Details.Floor": "$floor",
"Location.Address.Details.Unit": "$unit"
}
""")))
.containsExactly(json("""
_id: 1,
Company: 'Acme Corp',
Location: {
Country: 'USA',
Address: {
State: 'California',
City: 'San Francisco',
Details: {
Street: 'Market Street',
BuildingNumber: 123,
Floor: 5,
Unit: 'A'
}
}
}
"""));
}

// https://github.com/bwaldvogel/mongo-java-server/issues/191
@Test
void testProjectWithCondition() throws Exception {
Expand Down