Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-832-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-832-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-832-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-832-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-832-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-832-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -44,6 +43,7 @@
* @author Christoph Strobl
* @author Thomas Darimont
* @author Alexey Plotnik
* @author Mark Paluch
*/
public class Update {

Expand Down Expand Up @@ -603,6 +603,39 @@ public boolean equals(Object that) {
}
}

/**
* 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}.
*
Expand Down Expand Up @@ -656,6 +689,23 @@ public Update each(Object... values) {
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}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,24 @@ public void updatePushEachAtPositionWorksCorrectlyWhenGivenPositionNull() {
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
*/
Expand Down