From 4259bc85612e936a66de2410aeb8dbc7cb6720fe Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 28 May 2025 13:19:59 +0200 Subject: [PATCH 1/2] Prepare issue branch. --- pom.xml | 2 +- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 95fc8379d9..455fad1a45 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-mongodb-parent - 5.0.0-SNAPSHOT + 5.0.x-GH-4969-SNAPSHOT pom Spring Data MongoDB diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml index fc88571622..0f40ceb52f 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 - 5.0.0-SNAPSHOT + 5.0.x-GH-4969-SNAPSHOT ../pom.xml diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml index 6f34da5660..4d92d1cef9 100644 --- a/spring-data-mongodb/pom.xml +++ b/spring-data-mongodb/pom.xml @@ -13,7 +13,7 @@ org.springframework.data spring-data-mongodb-parent - 5.0.0-SNAPSHOT + 5.0.x-GH-4969-SNAPSHOT ../pom.xml From e61dba01d35513f244179bc3ba3007a23ae4183d Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 28 May 2025 15:09:42 +0200 Subject: [PATCH 2/2] Update $out stage rendering to new format. This commit makes sure to use the newer command format when rendering the $out aggregation stage. --- .../core/aggregation/OutOperation.java | 33 ++++++++++++++----- .../aggregation/OutOperationUnitTest.java | 14 ++++---- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/OutOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/OutOperation.java index 7dbed3a855..6a11c089ab 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/OutOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/OutOperation.java @@ -98,13 +98,15 @@ public OutOperation in(@Nullable String database) { * .uniqueKey("{ 'field-1' : 1, 'field-2' : 1}") * * - * NOTE: Requires MongoDB 4.2 or later. + * NOTE: Only suitable for 4.2+ to (not including) 5.0. * * @param key can be {@literal null}. Server uses {@literal _id} when {@literal null}. * @return new instance of {@link OutOperation}. * @since 2.2 + * @deprecated no longer applicable for MongoDB 5+ */ @Contract("_ -> new") + @Deprecated public OutOperation uniqueKey(@Nullable String key) { Document uniqueKey = key == null ? null : BsonUtils.toDocumentOrElse(key, it -> new Document(it, 1)); @@ -123,13 +125,15 @@ public OutOperation uniqueKey(@Nullable String key) { * .uniqueKeyOf(Arrays.asList("field-1", "field-2")) * * - * NOTE: Requires MongoDB 4.2 or later. + * NOTE: Only suitable for 4.2+ to (not including) 5.0. * * @param fields must not be {@literal null}. * @return new instance of {@link OutOperation}. * @since 2.2 + * @deprecated no longer applicable for MongoDB 5+ */ @Contract("_ -> new") + @Deprecated public OutOperation uniqueKeyOf(Iterable fields) { Assert.notNull(fields, "Fields must not be null"); @@ -142,13 +146,15 @@ public OutOperation uniqueKeyOf(Iterable fields) { /** * Specify how to merge the aggregation output with the target collection.
- * NOTE: Requires MongoDB 4.2 or later. + * NOTE: Only suitable for 4.2+ to (not including) 5.0. * * @param mode must not be {@literal null}. * @return new instance of {@link OutOperation}. * @since 2.2 + * @deprecated no longer applicable for MongoDB 5+ */ @Contract("_ -> new") + @Deprecated public OutOperation mode(OutMode mode) { Assert.notNull(mode, "Mode must not be null"); @@ -157,39 +163,45 @@ public OutOperation mode(OutMode mode) { /** * Replace the target collection.
- * NOTE: Requires MongoDB 4.2 or later. + * NOTE: Only suitable for 4.2+ to (not including) 5.0. * * @return new instance of {@link OutOperation}. * @see OutMode#REPLACE_COLLECTION * @since 2.2 + * @deprecated no longer applicable for MongoDB 5+ */ @Contract("-> new") + @Deprecated public OutOperation replaceCollection() { return mode(OutMode.REPLACE_COLLECTION); } /** * Replace/Upsert documents in the target collection.
- * NOTE: Requires MongoDB 4.2 or later. + * NOTE: Only suitable for 4.2+ to (not including) 5.0. * * @return new instance of {@link OutOperation}. * @see OutMode#REPLACE * @since 2.2 + * @deprecated no longer applicable for MongoDB 5+ */ @Contract("-> new") + @Deprecated public OutOperation replaceDocuments() { return mode(OutMode.REPLACE); } /** * Insert documents to the target collection.
- * NOTE: Requires MongoDB 4.2 or later. + * NOTE: Only suitable for 4.2+ to (not including) 5.0. * * @return new instance of {@link OutOperation}. * @see OutMode#INSERT * @since 2.2 + * @deprecated no longer applicable for MongoDB 5+ */ @Contract("-> new") + @Deprecated public OutOperation insertDocuments() { return mode(OutMode.INSERT); } @@ -198,7 +210,10 @@ public OutOperation insertDocuments() { public Document toDocument(AggregationOperationContext context) { if (!requiresMongoDb42Format()) { - return new Document("$out", collectionName); + if (!StringUtils.hasText(databaseName)) { + return new Document(getOperator(), collectionName); + } + return new Document(getOperator(), new Document("db", databaseName).append("coll", collectionName)); } Assert.state(mode != null, "Mode must not be null"); @@ -223,7 +238,7 @@ public String getOperator() { } private boolean requiresMongoDb42Format() { - return StringUtils.hasText(databaseName) || mode != null || uniqueKey != null; + return mode != null || uniqueKey != null; } /** @@ -231,7 +246,9 @@ private boolean requiresMongoDb42Format() { * * @author Christoph Strobl * @since 2.2 + * @deprecated no longer applicable for MongoDB 5+ */ + @Deprecated public enum OutMode { /** diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/OutOperationUnitTest.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/OutOperationUnitTest.java index f8812448b3..51f6b84f19 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/OutOperationUnitTest.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/OutOperationUnitTest.java @@ -15,8 +15,9 @@ */ package org.springframework.data.mongodb.core.aggregation; -import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; -import static org.springframework.data.mongodb.test.util.Assertions.*; +import static org.springframework.data.mongodb.core.aggregation.Aggregation.out; +import static org.springframework.data.mongodb.test.util.Assertions.assertThat; +import static org.springframework.data.mongodb.test.util.Assertions.assertThatIllegalArgumentException; import java.util.Arrays; @@ -84,11 +85,12 @@ public void shouldRenderExtendedFormatWithMultiFieldKey() { .containsEntry("$out.uniqueKey", new Document("field-1", 1).append("field-2", 1)); } - @Test // DATAMONGO-2259 - public void shouldErrorOnExtendedFormatWithoutMode() { + @Test // DATAMONGO-2259, GH-4969 + public void shouldRenderNewExtendedFormatWithoutMode() { - assertThatThrownBy(() -> out("out-col").in("database-2").toDocument(Aggregation.DEFAULT_CONTEXT)) - .isInstanceOf(IllegalStateException.class); + assertThat(out("out-col").in("database-2").toDocument(Aggregation.DEFAULT_CONTEXT)) + .containsEntry("$out.coll", "out-col") // + .containsEntry("$out.db", "database-2"); } }