Skip to content
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

Support Expressions in $slice operator #4858

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>4.5.0-SNAPSHOT</version>
<version>4.5.x-GH-4857-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
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 @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.5.0-SNAPSHOT</version>
<version>4.5.x-GH-4857-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 @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.5.0-SNAPSHOT</version>
<version>4.5.x-GH-4857-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1032,35 +1032,68 @@ public Slice itemCount(int nrElements) {
return new Slice(append(nrElements));
}

/**
* Slice the number of elements.
*
* @param nrElements An {@link AggregationExpression} that evaluates to a numeric value used as item count.
* @return new instance of {@link Slice}.
* @since 4.5
*/
public Slice itemCount(AggregationExpression nrElements) {
return new Slice(append(nrElements));
}

/**
* Slice using offset and count.
*
* @param position the start position
* @return new instance of {@link SliceElementsBuilder} to create {@link Slice}.
*/
public SliceElementsBuilder offset(final int position) {

return new SliceElementsBuilder() {
public SliceElementsBuilder offset(int position) {
return new SliceElementsBuilder(position);
}

@Override
public Slice itemCount(int nrElements) {
return new Slice(append(position)).itemCount(nrElements);
}
};
/**
* Slice using offset and count.
*
* @param position the start position
* @return new instance of {@link SliceElementsBuilder} to create {@link Slice}.
*/
public SliceElementsBuilder offset(AggregationExpression position) {
return new SliceElementsBuilder(position);
}

/**
* @author Christoph Strobl
*/
public interface SliceElementsBuilder {
public class SliceElementsBuilder {

private final Object position;

SliceElementsBuilder(Object position) {
this.position = position;
}

/**
* Set the number of elements given {@literal nrElements}.
*
* @param nrElements
* @return
* @return new instance of {@link Slice}.
*/
Slice itemCount(int nrElements);
public Slice itemCount(int nrElements) {
return new Slice(append(position)).itemCount(nrElements);
}

/**
* Slice the number of elements.
*
* @param nrElements An {@link AggregationExpression} that evaluates to a numeric value used as item count.
* @return new instance of {@link Slice}.
* @since 4.5
*/
public Slice itemCount(AggregationExpression nrElements) {
return new Slice(append(position)).itemCount(nrElements);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Range.Bound;
import org.springframework.data.mongodb.core.DocumentTestUtils;
import org.springframework.data.mongodb.core.aggregation.ArithmeticOperators.Subtract;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.Variable;
Expand Down Expand Up @@ -1064,6 +1065,17 @@ void shouldRenderSliceWithPositionAggregationExpression() {
assertThat(agg).isEqualTo(Document.parse("{ $project: { threeFavorites: { $slice: [ \"$favorites\", 2, 3 ] } } }"));
}

@Test // DATAMONGO-4857
void shouldRenderSliceWithExpressions() {

Document agg = project().and(ArrayOperators.arrayOf("favorites").slice()
.offset(Subtract.valueOf(ArrayOperators.Size.lengthOfArray("myArray")).subtract(1))
.itemCount(ArithmeticOperators.rand())).as("threeFavorites").toDocument(Aggregation.DEFAULT_CONTEXT);

assertThat(agg).isEqualTo(Document.parse(
"{ $project: { threeFavorites: { $slice: [ \"$favorites\", { \"$subtract\": [ {\"$size\": \"$myArray\"}, 1]}, { $rand : {} } ] } } }"));
}

@Test // DATAMONGO-1536
void shouldRenderLiteral() {

Expand Down
Loading