DATAMONGO-2055 - Allow position modifier to be negative using push at position on Update.
Original pull request: #600.
This commit is contained in:
committed by
Mark Paluch
parent
208bd6ae52
commit
67c3f02dcc
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user