From 548e9387bf11dbf032a80daa02d3fce077f42f1b Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 23 May 2022 13:24:33 +0200 Subject: [PATCH 1/2] Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-benchmarks/pom.xml | 2 +- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index b78554647f..42845b1f1c 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 4.1.0-SNAPSHOT + 4.1.0-GH-4043-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml index 1b2a1390e6..b32435a7d6 100644 --- a/spring-data-mongodb-benchmarks/pom.xml +++ b/spring-data-mongodb-benchmarks/pom.xml @@ -7,7 +7,7 @@ org.springframework.data spring-data-mongodb-parent - 4.1.0-SNAPSHOT + 4.1.0-GH-4043-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index 8db8d798fb..a49ac39f7a 100644 --- a/spring-data-mongodb-distribution/pom.xml +++ b/spring-data-mongodb-distribution/pom.xml @@ -15,7 +15,7 @@ org.springframework.data spring-data-mongodb-parent - 4.1.0-SNAPSHOT + 4.1.0-GH-4043-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 9a57f7eb52..ec4cdee226 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 4.1.0-SNAPSHOT + 4.1.0-GH-4043-SNAPSHOT ../pom.xml From 58be80b8d13dd661f0259b802427c6a81d3744d1 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 28 Nov 2022 11:13:25 +0100 Subject: [PATCH 2/2] Avoid multiple mapping iterations. A 2nd pass is no longer needed as the context already does all the work. --- .../data/mongodb/core/AggregationUtil.java | 19 ++-------- .../core/aggregation/AggregationTests.java | 35 ++++++++++++++++++- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/AggregationUtil.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/AggregationUtil.java index 7ba6f0d3b8..4ff3949c70 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/AggregationUtil.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/AggregationUtil.java @@ -32,7 +32,6 @@ import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; import org.springframework.data.util.Lazy; import org.springframework.lang.Nullable; -import org.springframework.util.ObjectUtils; /** * Utility methods to map {@link org.springframework.data.mongodb.core.aggregation.Aggregation} pipeline definitions and @@ -96,12 +95,7 @@ AggregationOperationContext createAggregationContext(Aggregation aggregation, @N * @return */ List createPipeline(Aggregation aggregation, AggregationOperationContext context) { - - if (ObjectUtils.nullSafeEquals(context, Aggregation.DEFAULT_CONTEXT)) { - return aggregation.toPipeline(context); - } - - return mapAggregationPipeline(aggregation.toPipeline(context)); + return aggregation.toPipeline(context); } /** @@ -112,16 +106,7 @@ List createPipeline(Aggregation aggregation, AggregationOperationConte * @return */ Document createCommand(String collection, Aggregation aggregation, AggregationOperationContext context) { - - Document command = aggregation.toDocument(collection, context); - - if (!ObjectUtils.nullSafeEquals(context, Aggregation.DEFAULT_CONTEXT)) { - return command; - } - - command.put("pipeline", mapAggregationPipeline(command.get("pipeline", List.class))); - - return command; + return aggregation.toDocument(collection, context); } private List mapAggregationPipeline(List pipeline) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java index 15c39b2817..d30d903ce8 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java @@ -43,11 +43,11 @@ import org.assertj.core.data.Offset; import org.bson.Document; +import org.bson.types.ObjectId; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; - import org.springframework.core.io.ClassPathResource; import org.springframework.data.annotation.Id; import org.springframework.data.domain.Sort; @@ -65,6 +65,7 @@ import org.springframework.data.mongodb.core.geo.GeoJsonPoint; import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; import org.springframework.data.mongodb.core.index.GeospatialIndex; +import org.springframework.data.mongodb.core.mapping.MongoId; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.NearQuery; import org.springframework.data.mongodb.core.query.Query; @@ -1933,6 +1934,24 @@ void mapsEnumsInMatchClauseUsingInCriteriaCorrectly() { assertThat(results.getMappedResults()).hasSize(1); } + @Test // GH-4043 + void considersMongoIdWithinTypedCollections() { + + UserRef userRef = new UserRef(); + userRef.id = "4ee921aca44fd11b3254e001"; + userRef.name = "u-1"; + + Widget widget = new Widget(); + widget.id = "w-1"; + widget.users = List.of(userRef); + + mongoTemplate.save(widget); + + Criteria criteria = Criteria.where("users").elemMatch(Criteria.where("id").is("4ee921aca44fd11b3254e001")); + AggregationResults aggregate = mongoTemplate.aggregate(newAggregation(match(criteria)), Widget.class, Widget.class); + assertThat(aggregate.getMappedResults()).contains(widget); + } + private void createUsersWithReferencedPersons() { mongoTemplate.dropCollection(User.class); @@ -2250,4 +2269,18 @@ static class WithEnum { @Id String id; MyEnum enumValue; } + + @lombok.Data + static class Widget { + @Id + String id; + List users; + } + + @lombok.Data + static class UserRef { + @MongoId + String id; + String name; + } }