Update $out stage rendering to documented format.

This commit makes sure to use the documented command format when rendering the $out aggregation stage.

Original pull request: #4986
Closes #4969
This commit is contained in:
Christoph Strobl
2025-05-28 15:09:42 +02:00
committed by Mark Paluch
parent 0e169aa23e
commit b4865e9965
2 changed files with 27 additions and 14 deletions

View File

@@ -96,11 +96,12 @@ public class OutOperation implements AggregationOperation {
* .uniqueKey("{ 'field-1' : 1, 'field-2' : 1}") * .uniqueKey("{ 'field-1' : 1, 'field-2' : 1}")
* </pre> * </pre>
* *
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later. * <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
* *
* @param key can be {@literal null}. Server uses {@literal _id} when {@literal null}. * @param key can be {@literal null}. Server uses {@literal _id} when {@literal null}.
* @return new instance of {@link OutOperation}. * @return new instance of {@link OutOperation}.
* @since 2.2 * @since 2.2
* @deprecated no longer applicable for MongoDB 5+
*/ */
public OutOperation uniqueKey(@Nullable String key) { public OutOperation uniqueKey(@Nullable String key) {
@@ -120,11 +121,12 @@ public class OutOperation implements AggregationOperation {
* .uniqueKeyOf(Arrays.asList("field-1", "field-2")) * .uniqueKeyOf(Arrays.asList("field-1", "field-2"))
* </pre> * </pre>
* *
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later. * <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
* *
* @param fields must not be {@literal null}. * @param fields must not be {@literal null}.
* @return new instance of {@link OutOperation}. * @return new instance of {@link OutOperation}.
* @since 2.2 * @since 2.2
* @deprecated no longer applicable for MongoDB 5+
*/ */
public OutOperation uniqueKeyOf(Iterable<String> fields) { public OutOperation uniqueKeyOf(Iterable<String> fields) {
@@ -138,11 +140,12 @@ public class OutOperation implements AggregationOperation {
/** /**
* Specify how to merge the aggregation output with the target collection. <br /> * Specify how to merge the aggregation output with the target collection. <br />
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later. * <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
* *
* @param mode must not be {@literal null}. * @param mode must not be {@literal null}.
* @return new instance of {@link OutOperation}. * @return new instance of {@link OutOperation}.
* @since 2.2 * @since 2.2
* @deprecated no longer applicable for MongoDB 5+
*/ */
public OutOperation mode(OutMode mode) { public OutOperation mode(OutMode mode) {
@@ -152,11 +155,12 @@ public class OutOperation implements AggregationOperation {
/** /**
* Replace the target collection. <br /> * Replace the target collection. <br />
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later. * <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
* *
* @return new instance of {@link OutOperation}. * @return new instance of {@link OutOperation}.
* @see OutMode#REPLACE_COLLECTION * @see OutMode#REPLACE_COLLECTION
* @since 2.2 * @since 2.2
* @deprecated no longer applicable for MongoDB 5+
*/ */
public OutOperation replaceCollection() { public OutOperation replaceCollection() {
return mode(OutMode.REPLACE_COLLECTION); return mode(OutMode.REPLACE_COLLECTION);
@@ -164,11 +168,12 @@ public class OutOperation implements AggregationOperation {
/** /**
* Replace/Upsert documents in the target collection. <br /> * Replace/Upsert documents in the target collection. <br />
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later. * <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
* *
* @return new instance of {@link OutOperation}. * @return new instance of {@link OutOperation}.
* @see OutMode#REPLACE * @see OutMode#REPLACE
* @since 2.2 * @since 2.2
* @deprecated no longer applicable for MongoDB 5+
*/ */
public OutOperation replaceDocuments() { public OutOperation replaceDocuments() {
return mode(OutMode.REPLACE); return mode(OutMode.REPLACE);
@@ -176,11 +181,12 @@ public class OutOperation implements AggregationOperation {
/** /**
* Insert documents to the target collection. <br /> * Insert documents to the target collection. <br />
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later. * <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
* *
* @return new instance of {@link OutOperation}. * @return new instance of {@link OutOperation}.
* @see OutMode#INSERT * @see OutMode#INSERT
* @since 2.2 * @since 2.2
* @deprecated no longer applicable for MongoDB 5+
*/ */
public OutOperation insertDocuments() { public OutOperation insertDocuments() {
return mode(OutMode.INSERT); return mode(OutMode.INSERT);
@@ -190,7 +196,10 @@ public class OutOperation implements AggregationOperation {
public Document toDocument(AggregationOperationContext context) { public Document toDocument(AggregationOperationContext context) {
if (!requiresMongoDb42Format()) { 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"); Assert.state(mode != null, "Mode must not be null");
@@ -215,7 +224,7 @@ public class OutOperation implements AggregationOperation {
} }
private boolean requiresMongoDb42Format() { private boolean requiresMongoDb42Format() {
return StringUtils.hasText(databaseName) || mode != null || uniqueKey != null; return mode != null || uniqueKey != null;
} }
/** /**
@@ -223,7 +232,9 @@ public class OutOperation implements AggregationOperation {
* *
* @author Christoph Strobl * @author Christoph Strobl
* @since 2.2 * @since 2.2
* @deprecated no longer applicable for MongoDB 5+
*/ */
@Deprecated
public enum OutMode { public enum OutMode {
/** /**

View File

@@ -15,8 +15,9 @@
*/ */
package org.springframework.data.mongodb.core.aggregation; package org.springframework.data.mongodb.core.aggregation;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; import static org.springframework.data.mongodb.core.aggregation.Aggregation.out;
import static org.springframework.data.mongodb.test.util.Assertions.*; import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
import static org.springframework.data.mongodb.test.util.Assertions.assertThatIllegalArgumentException;
import java.util.Arrays; import java.util.Arrays;
@@ -84,11 +85,12 @@ public class OutOperationUnitTest {
.containsEntry("$out.uniqueKey", new Document("field-1", 1).append("field-2", 1)); .containsEntry("$out.uniqueKey", new Document("field-1", 1).append("field-2", 1));
} }
@Test // DATAMONGO-2259 @Test // DATAMONGO-2259, GH-4969
public void shouldErrorOnExtendedFormatWithoutMode() { public void shouldRenderNewExtendedFormatWithoutMode() {
assertThatThrownBy(() -> out("out-col").in("database-2").toDocument(Aggregation.DEFAULT_CONTEXT)) assertThat(out("out-col").in("database-2").toDocument(Aggregation.DEFAULT_CONTEXT))
.isInstanceOf(IllegalStateException.class); .containsEntry("$out.coll", "out-col") //
.containsEntry("$out.db", "database-2");
} }
} }