Skip to content

Commit b4865e9

Browse files
christophstroblmp911de
authored andcommitted
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
1 parent 0e169aa commit b4865e9

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ public OutOperation in(@Nullable String database) {
9696
* .uniqueKey("{ 'field-1' : 1, 'field-2' : 1}")
9797
* </pre>
9898
*
99-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
99+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
100100
*
101101
* @param key can be {@literal null}. Server uses {@literal _id} when {@literal null}.
102102
* @return new instance of {@link OutOperation}.
103103
* @since 2.2
104+
* @deprecated no longer applicable for MongoDB 5+
104105
*/
105106
public OutOperation uniqueKey(@Nullable String key) {
106107

@@ -120,11 +121,12 @@ public OutOperation uniqueKey(@Nullable String key) {
120121
* .uniqueKeyOf(Arrays.asList("field-1", "field-2"))
121122
* </pre>
122123
*
123-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
124+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
124125
*
125126
* @param fields must not be {@literal null}.
126127
* @return new instance of {@link OutOperation}.
127128
* @since 2.2
129+
* @deprecated no longer applicable for MongoDB 5+
128130
*/
129131
public OutOperation uniqueKeyOf(Iterable<String> fields) {
130132

@@ -138,11 +140,12 @@ public OutOperation uniqueKeyOf(Iterable<String> fields) {
138140

139141
/**
140142
* Specify how to merge the aggregation output with the target collection. <br />
141-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
143+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
142144
*
143145
* @param mode must not be {@literal null}.
144146
* @return new instance of {@link OutOperation}.
145147
* @since 2.2
148+
* @deprecated no longer applicable for MongoDB 5+
146149
*/
147150
public OutOperation mode(OutMode mode) {
148151

@@ -152,35 +155,38 @@ public OutOperation mode(OutMode mode) {
152155

153156
/**
154157
* Replace the target collection. <br />
155-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
158+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
156159
*
157160
* @return new instance of {@link OutOperation}.
158161
* @see OutMode#REPLACE_COLLECTION
159162
* @since 2.2
163+
* @deprecated no longer applicable for MongoDB 5+
160164
*/
161165
public OutOperation replaceCollection() {
162166
return mode(OutMode.REPLACE_COLLECTION);
163167
}
164168

165169
/**
166170
* Replace/Upsert documents in the target collection. <br />
167-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
171+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
168172
*
169173
* @return new instance of {@link OutOperation}.
170174
* @see OutMode#REPLACE
171175
* @since 2.2
176+
* @deprecated no longer applicable for MongoDB 5+
172177
*/
173178
public OutOperation replaceDocuments() {
174179
return mode(OutMode.REPLACE);
175180
}
176181

177182
/**
178183
* Insert documents to the target collection. <br />
179-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
184+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
180185
*
181186
* @return new instance of {@link OutOperation}.
182187
* @see OutMode#INSERT
183188
* @since 2.2
189+
* @deprecated no longer applicable for MongoDB 5+
184190
*/
185191
public OutOperation insertDocuments() {
186192
return mode(OutMode.INSERT);
@@ -190,7 +196,10 @@ public OutOperation insertDocuments() {
190196
public Document toDocument(AggregationOperationContext context) {
191197

192198
if (!requiresMongoDb42Format()) {
193-
return new Document("$out", collectionName);
199+
if (!StringUtils.hasText(databaseName)) {
200+
return new Document(getOperator(), collectionName);
201+
}
202+
return new Document(getOperator(), new Document("db", databaseName).append("coll", collectionName));
194203
}
195204

196205
Assert.state(mode != null, "Mode must not be null");
@@ -215,15 +224,17 @@ public String getOperator() {
215224
}
216225

217226
private boolean requiresMongoDb42Format() {
218-
return StringUtils.hasText(databaseName) || mode != null || uniqueKey != null;
227+
return mode != null || uniqueKey != null;
219228
}
220229

221230
/**
222231
* The mode for merging the aggregation pipeline output.
223232
*
224233
* @author Christoph Strobl
225234
* @since 2.2
235+
* @deprecated no longer applicable for MongoDB 5+
226236
*/
237+
@Deprecated
227238
public enum OutMode {
228239

229240
/**

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/OutOperationUnitTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616
package org.springframework.data.mongodb.core.aggregation;
1717

18-
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
19-
import static org.springframework.data.mongodb.test.util.Assertions.*;
18+
import static org.springframework.data.mongodb.core.aggregation.Aggregation.out;
19+
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
20+
import static org.springframework.data.mongodb.test.util.Assertions.assertThatIllegalArgumentException;
2021

2122
import java.util.Arrays;
2223

@@ -84,11 +85,12 @@ public void shouldRenderExtendedFormatWithMultiFieldKey() {
8485
.containsEntry("$out.uniqueKey", new Document("field-1", 1).append("field-2", 1));
8586
}
8687

87-
@Test // DATAMONGO-2259
88-
public void shouldErrorOnExtendedFormatWithoutMode() {
88+
@Test // DATAMONGO-2259, GH-4969
89+
public void shouldRenderNewExtendedFormatWithoutMode() {
8990

90-
assertThatThrownBy(() -> out("out-col").in("database-2").toDocument(Aggregation.DEFAULT_CONTEXT))
91-
.isInstanceOf(IllegalStateException.class);
91+
assertThat(out("out-col").in("database-2").toDocument(Aggregation.DEFAULT_CONTEXT))
92+
.containsEntry("$out.coll", "out-col") //
93+
.containsEntry("$out.db", "database-2");
9294
}
9395

9496
}

0 commit comments

Comments
 (0)