diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java index 5cc0e4f7a..d42de50cd 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java @@ -805,18 +805,14 @@ public class Update { /** * Forces values to be added at the given {@literal position}. * - * @param position needs to be greater than or equal to zero. + * @param position the position offset. As of MongoDB 3.6 use a negative value to indicate starting from the end, + * counting (but not including) the last element of the array. * @return never {@literal null}. * @since 1.7 */ public PushOperatorBuilder atPosition(int position) { - if (position < 0) { - throw new IllegalArgumentException("Position must be greater than or equal to zero."); - } - this.modifiers.addModifier(new PositionModifier(position)); - return this; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java index 1325bb800..b0d5e879b 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/UpdateTests.java @@ -399,9 +399,11 @@ public class UpdateTests { equalTo(new BasicDBObjectBuilder().add("$bit", new BasicDBObject("key", new BasicDBObject("xor", 10L))).get())); } - @Test(expected = IllegalArgumentException.class) // DATAMONGO-943 - public void pushShouldThrowExceptionWhenGivenNegativePosition() { - new Update().push("foo").atPosition(-1).each("booh"); + @Test // DATAMONGO-943, // DATAMONGO-2055 + public void pushShouldAllowNegativePosition() { + + assertThat(new Update().push("foo").atPosition(-1).each("booh").toString()).isEqualTo( + "{ \"$push\" : { \"foo\" : { \"$java\" : { \"$position\" : { \"$java\" : { \"$position\" : -1} }, \"$each\" : { \"$java\" : { \"$each\" : [ \"booh\"]} } } } } }"); } @Test // DATAMONGO-1346