Skip to content

Commit b9d7206

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 c1647ed commit b9d7206

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
@@ -805,18 +805,14 @@ public PushOperatorBuilder sort(Sort sort) {
805805
/**
806806
* Forces values to be added at the given {@literal position}.
807807
*
808-
* @param position needs to be greater than or equal to zero.
808+
* @param position the position offset. As of MongoDB 3.6 use a negative value to indicate starting from the end,
809+
* counting (but not including) the last element of the array.
809810
* @return never {@literal null}.
810811
* @since 1.7
811812
*/
812813
public PushOperatorBuilder atPosition(int position) {
813814

814-
if (position < 0) {
815-
throw new IllegalArgumentException("Position must be greater than or equal to zero.");
816-
}
817-
818815
this.modifiers.addModifier(new PositionModifier(position));
819-
820816
return this;
821817
}
822818

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
@@ -399,9 +399,11 @@ public void getUpdateObjectShouldReturnCorrectRepresentationForBitwiseXor() {
399399
equalTo(new BasicDBObjectBuilder().add("$bit", new BasicDBObject("key", new BasicDBObject("xor", 10L))).get()));
400400
}
401401

402-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-943
403-
public void pushShouldThrowExceptionWhenGivenNegativePosition() {
404-
new Update().push("foo").atPosition(-1).each("booh");
402+
@Test // DATAMONGO-943, // DATAMONGO-2055
403+
public void pushShouldAllowNegativePosition() {
404+
405+
assertThat(new Update().push("foo").atPosition(-1).each("booh").toString()).isEqualTo(
406+
"{ \"$push\" : { \"foo\" : { \"$java\" : { \"$position\" : { \"$java\" : { \"$position\" : -1} }, \"$each\" : { \"$java\" : { \"$each\" : [ \"booh\"]} } } } } }");
405407
}
406408

407409
@Test // DATAMONGO-1346

0 commit comments

Comments
 (0)