Browse Source

DATAMONGO-832 - Add support for $slice in Update.push.

We now support $slice in Update operations via the PushOperatorBuilder.

    new Update().push("key").slice(5).each(Arrays.asList("one", "two", "three"));

Original Pull Request: #374
pull/375/merge
Mark Paluch 10 years ago committed by Christoph Strobl
parent
commit
026dce2612
  1. 52
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  2. 18
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

52
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

@ -20,7 +20,6 @@ import static org.springframework.util.ObjectUtils.*; @@ -20,7 +20,6 @@ import static org.springframework.util.ObjectUtils.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
@ -44,6 +43,7 @@ import com.mongodb.DBObject; @@ -44,6 +43,7 @@ import com.mongodb.DBObject;
* @author Christoph Strobl
* @author Thomas Darimont
* @author Alexey Plotnik
* @author Mark Paluch
*/
public class Update {
@ -603,6 +603,39 @@ public class Update { @@ -603,6 +603,39 @@ public class Update {
}
}
/**
* Implementation of {@link Modifier} representing {@code $slice}.
*
* @author Mark Paluch
* @since 1.10
*/
private static class Slice implements Modifier {
private int count;
public Slice(int count) {
this.count = count;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.query.Update.Modifier#getKey()
*/
@Override
public String getKey() {
return "$slice";
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.query.Update.Modifier#getValue()
*/
@Override
public Object getValue() {
return this.count;
}
}
/**
* {@link Modifier} implementation used to propagate {@code $position}.
*
@ -656,6 +689,23 @@ public class Update { @@ -656,6 +689,23 @@ public class Update {
return Update.this.push(key, this.modifiers);
}
/**
* Propagates {@code $slice} to {@code $push}. {@code $slice} requires the {@code $each operator}. <br />
* If {@literal count} is zero, {@code $slice} updates the array to an empty array. <br />
* If {@literal count} is negative, {@code $slice} updates the array to contain only the last {@code count}
* elements. <br />
* If {@literal count} is positive, {@code $slice} updates the array to contain only the first {@code count}
* elements. <br />
*
* @param count
* @return
*/
public PushOperatorBuilder slice(int count) {
this.modifiers.addModifier(new Slice(count));
return this;
}
/**
* Forces values to be added at the given {@literal position}.
*

18
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

@ -373,6 +373,24 @@ public class UpdateMapperUnitTests { @@ -373,6 +373,24 @@ public class UpdateMapperUnitTests {
assertThat(getAsDBObject(push, "key").containsField("$each"), is(true));
}
/**
* @see DATAMONGO-832
*/
@Test
public void updatePushEachWithSliceShouldRenderCorrectly() {
Update update = new Update().push("key").slice(5).each(Arrays.asList("Arya", "Arry", "Weasel"));
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
DBObject push = getAsDBObject(mappedObject, "$push");
DBObject key = getAsDBObject(push, "key");
assertThat(key.containsField("$slice"), is(true));
assertThat((Integer) key.get("$slice"), is(5));
assertThat(getAsDBObject(push, "key").containsField("$each"), is(true));
}
/**
* @see DATAMONGO-410
*/

Loading…
Cancel
Save