Skip to content

Commit 7f39691

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 961f800 commit 7f39691

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ 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
*/
106+
@Deprecated
105107
public OutOperation uniqueKey(@Nullable String key) {
106108

107109
Document uniqueKey = key == null ? null : BsonUtils.toDocumentOrElse(key, it -> new Document(it, 1));
@@ -120,12 +122,14 @@ public OutOperation uniqueKey(@Nullable String key) {
120122
* .uniqueKeyOf(Arrays.asList("field-1", "field-2"))
121123
* </pre>
122124
*
123-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
125+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
124126
*
125127
* @param fields must not be {@literal null}.
126128
* @return new instance of {@link OutOperation}.
127129
* @since 2.2
130+
* @deprecated no longer applicable for MongoDB 5+
128131
*/
132+
@Deprecated
129133
public OutOperation uniqueKeyOf(Iterable<String> fields) {
130134

131135
Assert.notNull(fields, "Fields must not be null");
@@ -138,12 +142,14 @@ public OutOperation uniqueKeyOf(Iterable<String> fields) {
138142

139143
/**
140144
* Specify how to merge the aggregation output with the target collection. <br />
141-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
145+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
142146
*
143147
* @param mode must not be {@literal null}.
144148
* @return new instance of {@link OutOperation}.
145149
* @since 2.2
150+
* @deprecated no longer applicable for MongoDB 5+
146151
*/
152+
@Deprecated
147153
public OutOperation mode(OutMode mode) {
148154

149155
Assert.notNull(mode, "Mode must not be null");
@@ -152,36 +158,42 @@ public OutOperation mode(OutMode mode) {
152158

153159
/**
154160
* Replace the target collection. <br />
155-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
161+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
156162
*
157163
* @return new instance of {@link OutOperation}.
158164
* @see OutMode#REPLACE_COLLECTION
159165
* @since 2.2
166+
* @deprecated no longer applicable for MongoDB 5+
160167
*/
168+
@Deprecated
161169
public OutOperation replaceCollection() {
162170
return mode(OutMode.REPLACE_COLLECTION);
163171
}
164172

165173
/**
166174
* Replace/Upsert documents in the target collection. <br />
167-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
175+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
168176
*
169177
* @return new instance of {@link OutOperation}.
170178
* @see OutMode#REPLACE
171179
* @since 2.2
180+
* @deprecated no longer applicable for MongoDB 5+
172181
*/
182+
@Deprecated
173183
public OutOperation replaceDocuments() {
174184
return mode(OutMode.REPLACE);
175185
}
176186

177187
/**
178188
* Insert documents to the target collection. <br />
179-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
189+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
180190
*
181191
* @return new instance of {@link OutOperation}.
182192
* @see OutMode#INSERT
183193
* @since 2.2
194+
* @deprecated no longer applicable for MongoDB 5+
184195
*/
196+
@Deprecated
185197
public OutOperation insertDocuments() {
186198
return mode(OutMode.INSERT);
187199
}
@@ -190,7 +202,10 @@ public OutOperation insertDocuments() {
190202
public Document toDocument(AggregationOperationContext context) {
191203

192204
if (!requiresMongoDb42Format()) {
193-
return new Document("$out", collectionName);
205+
if (!StringUtils.hasText(databaseName)) {
206+
return new Document(getOperator(), collectionName);
207+
}
208+
return new Document(getOperator(), new Document("db", databaseName).append("coll", collectionName));
194209
}
195210

196211
Assert.state(mode != null, "Mode must not be null");
@@ -215,15 +230,17 @@ public String getOperator() {
215230
}
216231

217232
private boolean requiresMongoDb42Format() {
218-
return StringUtils.hasText(databaseName) || mode != null || uniqueKey != null;
233+
return mode != null || uniqueKey != null;
219234
}
220235

221236
/**
222237
* The mode for merging the aggregation pipeline output.
223238
*
224239
* @author Christoph Strobl
225240
* @since 2.2
241+
* @deprecated no longer applicable for MongoDB 5+
226242
*/
243+
@Deprecated
227244
public enum OutMode {
228245

229246
/**

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)