Skip to content

Commit 026dce2

Browse files
mp911dechristophstrobl
authored andcommitted
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
1 parent eae32be commit 026dce2

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Arrays;
2121
import java.util.Collection;
2222
import java.util.Collections;
23-
import java.util.Date;
2423
import java.util.HashSet;
2524
import java.util.LinkedHashMap;
2625
import java.util.List;
@@ -44,6 +43,7 @@
4443
* @author Christoph Strobl
4544
* @author Thomas Darimont
4645
* @author Alexey Plotnik
46+
* @author Mark Paluch
4747
*/
4848
public class Update {
4949

@@ -603,6 +603,39 @@ public boolean equals(Object that) {
603603
}
604604
}
605605

606+
/**
607+
* Implementation of {@link Modifier} representing {@code $slice}.
608+
*
609+
* @author Mark Paluch
610+
* @since 1.10
611+
*/
612+
private static class Slice implements Modifier {
613+
614+
private int count;
615+
616+
public Slice(int count) {
617+
this.count = count;
618+
}
619+
620+
/*
621+
* (non-Javadoc)
622+
* @see org.springframework.data.mongodb.core.query.Update.Modifier#getKey()
623+
*/
624+
@Override
625+
public String getKey() {
626+
return "$slice";
627+
}
628+
629+
/*
630+
* (non-Javadoc)
631+
* @see org.springframework.data.mongodb.core.query.Update.Modifier#getValue()
632+
*/
633+
@Override
634+
public Object getValue() {
635+
return this.count;
636+
}
637+
}
638+
606639
/**
607640
* {@link Modifier} implementation used to propagate {@code $position}.
608641
*
@@ -656,6 +689,23 @@ public Update each(Object... values) {
656689
return Update.this.push(key, this.modifiers);
657690
}
658691

692+
/**
693+
* Propagates {@code $slice} to {@code $push}. {@code $slice} requires the {@code $each operator}. <br />
694+
* If {@literal count} is zero, {@code $slice} updates the array to an empty array. <br />
695+
* If {@literal count} is negative, {@code $slice} updates the array to contain only the last {@code count}
696+
* elements. <br />
697+
* If {@literal count} is positive, {@code $slice} updates the array to contain only the first {@code count}
698+
* elements. <br />
699+
*
700+
* @param count
701+
* @return
702+
*/
703+
public PushOperatorBuilder slice(int count) {
704+
705+
this.modifiers.addModifier(new Slice(count));
706+
return this;
707+
}
708+
659709
/**
660710
* Forces values to be added at the given {@literal position}.
661711
*

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,24 @@ public void updatePushEachAtPositionWorksCorrectlyWhenGivenPositionNull() {
373373
assertThat(getAsDBObject(push, "key").containsField("$each"), is(true));
374374
}
375375

376+
/**
377+
* @see DATAMONGO-832
378+
*/
379+
@Test
380+
public void updatePushEachWithSliceShouldRenderCorrectly() {
381+
382+
Update update = new Update().push("key").slice(5).each(Arrays.asList("Arya", "Arry", "Weasel"));
383+
384+
DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
385+
386+
DBObject push = getAsDBObject(mappedObject, "$push");
387+
DBObject key = getAsDBObject(push, "key");
388+
389+
assertThat(key.containsField("$slice"), is(true));
390+
assertThat((Integer) key.get("$slice"), is(5));
391+
assertThat(getAsDBObject(push, "key").containsField("$each"), is(true));
392+
}
393+
376394
/**
377395
* @see DATAMONGO-410
378396
*/

0 commit comments

Comments
 (0)