DATAMONGO-2055 - Allow position modifier to be negative using push at position on Update.

Original pull request: #600.
This commit is contained in:
Christoph Strobl
2018-08-13 13:31:13 +02:00
committed by Mark Paluch
parent c1647ed269
commit b9d72060af
2 changed files with 7 additions and 9 deletions

View File

@@ -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;
}

View File

@@ -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