Skip to content

Commit 67c3f02

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 208bd6a commit 67c3f02

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
@@ -893,18 +893,14 @@ public PushOperatorBuilder sort(Sort sort) {
893893
/**
894894
* Forces values to be added at the given {@literal position}.
895895
*
896-
* @param position needs to be greater than or equal to zero.
896+
* @param position the position offset. As of MongoDB 3.6 use a negative value to indicate starting from the end,
897+
* counting (but not including) the last element of the array.
897898
* @return never {@literal null}.
898899
* @since 1.7
899900
*/
900901
public PushOperatorBuilder atPosition(int position) {
901902

902-
if (position < 0) {
903-
throw new IllegalArgumentException("Position must be greater than or equal to zero.");
904-
}
905-
906903
this.modifiers.addModifier(new PositionModifier(position));
907-
908904
return this;
909905
}
910906

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)