Skip to content

Commit 861c827

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2055 - Allow position modifier to be negative using push at position on Update.
Original pull request: #600.
1 parent e545787 commit 861c827

File tree

2 files changed

+7
-9
lines changed
  • spring-data-mongodb/src

2 files changed

+7
-9
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -890,18 +890,14 @@ public PushOperatorBuilder sort(Sort sort) {
890890
/**
891891
* Forces values to be added at the given {@literal position}.
892892
*
893-
* @param position needs to be greater than or equal to zero.
893+
* @param position the position offset. As of MongoDB 3.6 use a negative value to indicate starting from the end,
894+
* counting (but not including) the last element of the array.
894895
* @return never {@literal null}.
895896
* @since 1.7
896897
*/
897898
public PushOperatorBuilder atPosition(int position) {
898899

899-
if (position < 0) {
900-
throw new IllegalArgumentException("Position must be greater than or equal to zero.");
901-
}
902-
903900
this.modifiers.addModifier(new PositionModifier(position));
904-
905901
return this;
906902
}
907903

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,11 @@ public void getUpdateObjectShouldReturnCorrectRepresentationForBitwiseXor() {
390390
.isEqualTo(new Document().append("$bit", new Document("key", new Document("xor", 10L))));
391391
}
392392

393-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-943
394-
public void pushShouldThrowExceptionWhenGivenNegativePosition() {
395-
new Update().push("foo").atPosition(-1).each("booh");
393+
@Test // DATAMONGO-943, // DATAMONGO-2055
394+
public void pushShouldAllowNegativePosition() {
395+
396+
assertThat(new Update().push("foo").atPosition(-1).each("booh").toString()).isEqualTo(
397+
"{ \"$push\" : { \"foo\" : { \"$java\" : { \"$position\" : { \"$java\" : { \"$position\" : -1} }, \"$each\" : { \"$java\" : { \"$each\" : [ \"booh\"]} } } } } }");
396398
}
397399

398400
@Test // DATAMONGO-1346

0 commit comments

Comments
 (0)