From 026dce26126036a6878f8ddab1045ac9537bca5c Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Fri, 8 Jul 2016 11:47:33 +0200 Subject: [PATCH] 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 --- .../data/mongodb/core/query/Update.java | 52 ++++++++++++++++++- .../core/convert/UpdateMapperUnitTests.java | 18 +++++++ 2 files changed, 69 insertions(+), 1 deletion(-) 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 8438e201b..39177729f 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 @@ -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; * @author Christoph Strobl * @author Thomas Darimont * @author Alexey Plotnik + * @author Mark Paluch */ 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 { return Update.this.push(key, this.modifiers); } + /** + * Propagates {@code $slice} to {@code $push}. {@code $slice} requires the {@code $each operator}.
+ * If {@literal count} is zero, {@code $slice} updates the array to an empty array.
+ * If {@literal count} is negative, {@code $slice} updates the array to contain only the last {@code count} + * elements.
+ * If {@literal count} is positive, {@code $slice} updates the array to contain only the first {@code count} + * elements.
+ * + * @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}. * diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java index 5d3d74858..81b8b69aa 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java @@ -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 */