Skip to content

DATAMONGO-1457 - Add support for $slice in aggregation. #372

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-1457-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-1457-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-1457-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-1457-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-1457-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-1457-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -352,6 +352,7 @@ protected static Object toMongoExpression(AggregationOperationContext context, S
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
public static class ProjectionOperationBuilder extends AbstractProjectionOperationBuilder {

Expand Down Expand Up @@ -566,6 +567,31 @@ public ProjectionOperationBuilder size() {
return project("size");
}

/**
* Generates a {@code $slice} expression that returns a subset of the array held by the given field. <br />
* If {@literal n} is positive, $slice returns up to the first n elements in the array. <br />
* If {@literal n} is negative, $slice returns up to the last n elements in the array.
*
* @param count max number of elements.
* @return never {@literal null}.
* @since 1.10
*/
public ProjectionOperationBuilder slice(int count) {
return project("slice", count);
}

/**
* Generates a {@code $slice} expression that returns a subset of the array held by the given field. <br />
*
* @param count max number of elements. Must not be negative.
* @param offset the offset within the array to start from.
* @return never {@literal null}.
* @since 1.10
*/
public ProjectionOperationBuilder slice(int count, int offset) {
return project("slice", offset, count);
}

/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,25 @@ public void outShouldOutBeTheLastOperation() {
skip(100));
}

/**
* @see DATAMONGO-1457
*/
@Test
public void sliceShouldBeAppliedCorrectly() {

createUserWithLikesDocuments();

TypedAggregation<UserWithLikes> agg = newAggregation(UserWithLikes.class, match(new Criteria()),
project().and("likes").slice(2));

AggregationResults<UserWithLikes> result = mongoTemplate.aggregate(agg, UserWithLikes.class);

assertThat(result.getMappedResults(), hasSize(9));
for (UserWithLikes user : result) {
assertThat(user.likes.size() <= 2, is(true));
}
}

private void createUsersWithReferencedPersons() {

mongoTemplate.dropCollection(User.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,6 +36,7 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
*/
public class ProjectionOperationUnitTests {

Expand Down Expand Up @@ -371,6 +372,36 @@ public void shouldRenderGenericSizeExpressionInProjection() {
assertThat(projected.get("tags_count"), is((Object) new BasicDBObject("$size", Arrays.asList("$tags"))));
}

/**
* @see DATAMONGO-1457
*/
@Test
public void shouldRenderSliceCorrectly() throws Exception {

ProjectionOperation operation = Aggregation.project().and("field").slice(10).as("renamed");

DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
DBObject projected = exctractOperation("$project", dbObject);

assertThat(projected.get("renamed"),
is((Object) new BasicDBObject("$slice", Arrays.<Object> asList("$field", 10))));
}

/**
* @see DATAMONGO-1457
*/
@Test
public void shouldRenderSliceWithPositionCorrectly() throws Exception {

ProjectionOperation operation = Aggregation.project().and("field").slice(10, 5).as("renamed");

DBObject dbObject = operation.toDBObject(Aggregation.DEFAULT_CONTEXT);
DBObject projected = exctractOperation("$project", dbObject);

assertThat(projected.get("renamed"),
is((Object) new BasicDBObject("$slice", Arrays.<Object> asList("$field", 5, 10))));
}

private static DBObject exctractOperation(String field, DBObject fromProjectClause) {
return (DBObject) fromProjectClause.get(field);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,16 +20,23 @@
import java.util.HashSet;
import java.util.Set;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
* @author Thomas Darimont
* @author Christoph Strobl
*/
@Data
@NoArgsConstructor
public class UserWithLikes {

String id;
Date joined;
Set<String> likes = new HashSet<String>();

public UserWithLikes(String id, Date joined, String... likes) {

this.id = id;
this.joined = joined;
this.likes = new HashSet<String>(Arrays.asList(likes));
Expand Down