Skip to content

Commit e61dba0

Browse files
committedMay 28, 2025··
Update $out stage rendering to new format.
This commit makes sure to use the newer command format when rendering the $out aggregation stage.
1 parent 4259bc8 commit e61dba0

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
@@ -98,13 +98,15 @@ public OutOperation in(@Nullable String database) {
9898
* .uniqueKey("{ 'field-1' : 1, 'field-2' : 1}")
9999
* </pre>
100100
*
101-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
101+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
102102
*
103103
* @param key can be {@literal null}. Server uses {@literal _id} when {@literal null}.
104104
* @return new instance of {@link OutOperation}.
105105
* @since 2.2
106+
* @deprecated no longer applicable for MongoDB 5+
106107
*/
107108
@Contract("_ -> new")
109+
@Deprecated
108110
public OutOperation uniqueKey(@Nullable String key) {
109111

110112
Document uniqueKey = key == null ? null : BsonUtils.toDocumentOrElse(key, it -> new Document(it, 1));
@@ -123,13 +125,15 @@ public OutOperation uniqueKey(@Nullable String key) {
123125
* .uniqueKeyOf(Arrays.asList("field-1", "field-2"))
124126
* </pre>
125127
*
126-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
128+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
127129
*
128130
* @param fields must not be {@literal null}.
129131
* @return new instance of {@link OutOperation}.
130132
* @since 2.2
133+
* @deprecated no longer applicable for MongoDB 5+
131134
*/
132135
@Contract("_ -> new")
136+
@Deprecated
133137
public OutOperation uniqueKeyOf(Iterable<String> fields) {
134138

135139
Assert.notNull(fields, "Fields must not be null");
@@ -142,13 +146,15 @@ public OutOperation uniqueKeyOf(Iterable<String> fields) {
142146

143147
/**
144148
* Specify how to merge the aggregation output with the target collection. <br />
145-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
149+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
146150
*
147151
* @param mode must not be {@literal null}.
148152
* @return new instance of {@link OutOperation}.
149153
* @since 2.2
154+
* @deprecated no longer applicable for MongoDB 5+
150155
*/
151156
@Contract("_ -> new")
157+
@Deprecated
152158
public OutOperation mode(OutMode mode) {
153159

154160
Assert.notNull(mode, "Mode must not be null");
@@ -157,39 +163,45 @@ public OutOperation mode(OutMode mode) {
157163

158164
/**
159165
* Replace the target collection. <br />
160-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
166+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
161167
*
162168
* @return new instance of {@link OutOperation}.
163169
* @see OutMode#REPLACE_COLLECTION
164170
* @since 2.2
171+
* @deprecated no longer applicable for MongoDB 5+
165172
*/
166173
@Contract("-> new")
174+
@Deprecated
167175
public OutOperation replaceCollection() {
168176
return mode(OutMode.REPLACE_COLLECTION);
169177
}
170178

171179
/**
172180
* Replace/Upsert documents in the target collection. <br />
173-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
181+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
174182
*
175183
* @return new instance of {@link OutOperation}.
176184
* @see OutMode#REPLACE
177185
* @since 2.2
186+
* @deprecated no longer applicable for MongoDB 5+
178187
*/
179188
@Contract("-> new")
189+
@Deprecated
180190
public OutOperation replaceDocuments() {
181191
return mode(OutMode.REPLACE);
182192
}
183193

184194
/**
185195
* Insert documents to the target collection. <br />
186-
* <strong>NOTE:</strong> Requires MongoDB 4.2 or later.
196+
* <strong>NOTE:</strong> Only suitable for 4.2+ to (not including) 5.0.
187197
*
188198
* @return new instance of {@link OutOperation}.
189199
* @see OutMode#INSERT
190200
* @since 2.2
201+
* @deprecated no longer applicable for MongoDB 5+
191202
*/
192203
@Contract("-> new")
204+
@Deprecated
193205
public OutOperation insertDocuments() {
194206
return mode(OutMode.INSERT);
195207
}
@@ -198,7 +210,10 @@ public OutOperation insertDocuments() {
198210
public Document toDocument(AggregationOperationContext context) {
199211

200212
if (!requiresMongoDb42Format()) {
201-
return new Document("$out", collectionName);
213+
if (!StringUtils.hasText(databaseName)) {
214+
return new Document(getOperator(), collectionName);
215+
}
216+
return new Document(getOperator(), new Document("db", databaseName).append("coll", collectionName));
202217
}
203218

204219
Assert.state(mode != null, "Mode must not be null");
@@ -223,15 +238,17 @@ public String getOperator() {
223238
}
224239

225240
private boolean requiresMongoDb42Format() {
226-
return StringUtils.hasText(databaseName) || mode != null || uniqueKey != null;
241+
return mode != null || uniqueKey != null;
227242
}
228243

229244
/**
230245
* The mode for merging the aggregation pipeline output.
231246
*
232247
* @author Christoph Strobl
233248
* @since 2.2
249+
* @deprecated no longer applicable for MongoDB 5+
234250
*/
251+
@Deprecated
235252
public enum OutMode {
236253

237254
/**

‎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)
Please sign in to comment.