Browse Source

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
4.4.x
Christoph Strobl 7 months ago committed by Mark Paluch
parent
commit
4ec26ff745
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 33
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/OutOperation.java
  2. 14
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/OutOperationUnitTest.java

33
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/OutOperation.java

@ -96,12 +96,14 @@ 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+
*/ */
@Deprecated
public OutOperation uniqueKey(@Nullable String key) { public OutOperation uniqueKey(@Nullable String key) {
Document uniqueKey = key == null ? null : BsonUtils.toDocumentOrElse(key, it -> new Document(it, 1)); Document uniqueKey = key == null ? null : BsonUtils.toDocumentOrElse(key, it -> new Document(it, 1));
@ -120,12 +122,14 @@ 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+
*/ */
@Deprecated
public OutOperation uniqueKeyOf(Iterable<String> fields) { public OutOperation uniqueKeyOf(Iterable<String> fields) {
Assert.notNull(fields, "Fields must not be null"); Assert.notNull(fields, "Fields must not be null");
@ -138,12 +142,14 @@ 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+
*/ */
@Deprecated
public OutOperation mode(OutMode mode) { public OutOperation mode(OutMode mode) {
Assert.notNull(mode, "Mode must not be null"); Assert.notNull(mode, "Mode must not be null");
@ -152,36 +158,42 @@ 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+
*/ */
@Deprecated
public OutOperation replaceCollection() { public OutOperation replaceCollection() {
return mode(OutMode.REPLACE_COLLECTION); return mode(OutMode.REPLACE_COLLECTION);
} }
/** /**
* 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+
*/ */
@Deprecated
public OutOperation replaceDocuments() { public OutOperation replaceDocuments() {
return mode(OutMode.REPLACE); return mode(OutMode.REPLACE);
} }
/** /**
* 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+
*/ */
@Deprecated
public OutOperation insertDocuments() { public OutOperation insertDocuments() {
return mode(OutMode.INSERT); return mode(OutMode.INSERT);
} }
@ -190,7 +202,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 +230,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 +238,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 {
/** /**

14
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; 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");
} }
} }

Loading…
Cancel
Save