Skip to content

Commit d748500

Browse files
committed
UpdateDefinition.inc(…) now returns this.
Align with other update methods returning the update definition. Closes #5045
1 parent 74144b5 commit d748500

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ public boolean modifies(String key) {
135135
}
136136

137137
@Override
138-
public void inc(String version) {
138+
public UpdateDefinition inc(String version) {
139139
delegate.inc(version);
140+
return this;
140141
}
141142

142143
@Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,10 @@ public boolean modifies(String key) {
263263
}
264264

265265
@Override
266-
public void inc(String key) {
266+
public UpdateDefinition inc(String key) {
267+
267268
set(new SetOperation(key, ArithmeticOperators.valueOf(key).add(1)));
269+
return this;
268270
}
269271

270272
@Override

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Update unset(String key) {
150150
}
151151

152152
/**
153-
* Update using the {@literal $inc} update modifier
153+
* Update using the {@literal $inc} update modifier.
154154
*
155155
* @param key the field name.
156156
* @param inc must not be {@literal null}.
@@ -163,14 +163,21 @@ public Update inc(String key, Number inc) {
163163
return this;
164164
}
165165

166+
/**
167+
* Update using the {@literal $inc} update modifier by one.
168+
*
169+
* @param key the field name.
170+
* @return this.
171+
* @see <a href="https://docs.mongodb.org/manual/reference/operator/update/inc/">MongoDB Update operator: $inc</a>
172+
*/
166173
@Override
167174
@Contract("_ -> this")
168-
public void inc(String key) {
169-
inc(key, 1L);
175+
public Update inc(String key) {
176+
return inc(key, 1L);
170177
}
171178

172179
/**
173-
* Update using the {@literal $push} update modifier
180+
* Update using the {@literal $push} update modifier.
174181
*
175182
* @param key the field name.
176183
* @param value can be {@literal null}.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/UpdateDefinition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ public interface UpdateDefinition {
5454
* Increment the value of a given {@literal key} by {@code 1}.
5555
*
5656
* @param key must not be {@literal null}.
57+
* @return {@code this} update definition.
5758
*/
58-
void inc(String key);
59+
UpdateDefinition inc(String key);
5960

6061
/**
6162
* Get the specification which elements to modify in an array field. {@link ArrayFilter} are passed directly to the

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/messaging/ChangeStreamTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
import org.junit.jupiter.api.BeforeEach;
3939
import org.junit.jupiter.api.Disabled;
4040
import org.junit.jupiter.api.Test;
41-
4241
import org.junitpioneer.jupiter.RetryingTest;
42+
4343
import org.springframework.data.annotation.Id;
4444
import org.springframework.data.mongodb.core.ChangeStreamOptions;
4545
import org.springframework.data.mongodb.core.CollectionOptions;
@@ -522,7 +522,7 @@ void filterOnUpdateDescriptionElement() throws InterruptedException {
522522
template.update(User.class).matching(query(where("id").is(jellyBelly.id)))
523523
.apply(Update.update("address", new Address("candy ave"))).first();
524524

525-
template.update(User.class).matching(query(where("id").is(sugarSplashy.id))).apply(new Update().inc("age", 1))
525+
template.update(User.class).matching(query(where("id").is(sugarSplashy.id))).apply(new Update().inc("age"))
526526
.first();
527527

528528
template.update(User.class).matching(query(where("id").is(huffyFluffy.id)))

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* @author Christoph Strobl
3636
* @author Thomas Darimont
3737
* @author Alexey Plotnik
38+
* @author Mark Paluch
3839
*/
3940
public class UpdateTests {
4041

@@ -64,8 +65,9 @@ public void testInc() {
6465
@Test
6566
public void testIncInc() {
6667

67-
Update u = new Update().inc("size", 1).inc("count", 1);
68-
assertThat(u.getUpdateObject()).isEqualTo(Document.parse("{ \"$inc\" : { \"size\" : 1 , \"count\" : 1}}"));
68+
Update u = new Update().inc("size").inc("count", 1);
69+
assertThat(u.getUpdateObject())
70+
.isEqualTo(Document.parse("{ \"$inc\" : { \"size\" : NumberLong(1) , \"count\" : 1}}"));
6971
}
7072

7173
@Test

0 commit comments

Comments
 (0)