From 67c3f02dcc41c79fd0ea76ed7aaec91b33410eaf Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 13 Aug 2018 13:31:13 +0200 Subject: [PATCH] DATAMONGO-2055 - Allow position modifier to be negative using push at position on Update. Original pull request: #600. --- .../springframework/data/mongodb/core/query/Update.java | 8 ++------ .../data/mongodb/core/query/UpdateTests.java | 8 +++++--- 2 files changed, 7 insertions(+), 9 deletions(-) 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 a96e4c9f6..64302c877 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 @@ -893,18 +893,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 1340e8324..f467bcc67 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 @@ -390,9 +390,11 @@ public class UpdateTests { .isEqualTo(new Document().append("$bit", new Document("key", new Document("xor", 10L)))); } - @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