DATAMONGO-784 - Add support for comparison aggregation operators to group & project.
We now directly support comparison aggregation operators ($cmp, $eq, $gt, $gte, $lt, $lte and $ne) on both group and project stages. Original pull request: #414.
This commit is contained in:
committed by
Mark Paluch
parent
3dc1e9355a
commit
e631e2d7c5
@@ -33,7 +33,7 @@ import com.mongodb.DBObject;
|
||||
*/
|
||||
public enum AggregationFunctionExpressions {
|
||||
|
||||
SIZE, GTE;
|
||||
SIZE, CMP, EQ, GT, GTE, LT, LTE, NE;
|
||||
|
||||
/**
|
||||
* Returns an {@link AggregationExpression} build from the current {@link Enum} name and the given parameters.
|
||||
|
||||
@@ -616,6 +616,78 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
|
||||
return project("size");
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@code $cmp} expression (compare to) that compares the value of the field to a given value or field.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 1.10
|
||||
*/
|
||||
public ProjectionOperationBuilder cmp(Object compareValue) {
|
||||
return project("cmp", compareValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@code $eq} expression (equal) that compares the value of the field to a given value or field.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 1.10
|
||||
*/
|
||||
public ProjectionOperationBuilder eq(Object compareValue) {
|
||||
return project("eq", compareValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@code $gt} expression (greater than) that compares the value of the field to a given value or field.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 1.10
|
||||
*/
|
||||
public ProjectionOperationBuilder gt(Object compareValue) {
|
||||
return project("gt", compareValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@code $gte} expression (greater than equal) that compares the value of the field to a given value or
|
||||
* field.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 1.10
|
||||
*/
|
||||
public ProjectionOperationBuilder gte(Object compareValue) {
|
||||
return project("gte", compareValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@code $lt} expression (less than) that compares the value of the field to a given value or field.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 1.10
|
||||
*/
|
||||
public ProjectionOperationBuilder lt(Object compareValue) {
|
||||
return project("lt", compareValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@code $lte} expression (less than equal) that compares the value of the field to a given value or
|
||||
* field.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 1.10
|
||||
*/
|
||||
public ProjectionOperationBuilder lte(Object compareValue) {
|
||||
return project("lte", compareValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a {@code $ne} expression (not equal) that compares the value of the field to a given value or field.
|
||||
*
|
||||
* @return never {@literal null}.
|
||||
* @since 1.10
|
||||
*/
|
||||
public ProjectionOperationBuilder ne(Object compareValue) {
|
||||
return project("ne", compareValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 />
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.mongodb.core.aggregation.AggregationFunctionExpressions.*;
|
||||
import static org.springframework.data.mongodb.core.aggregation.Fields.*;
|
||||
import static org.springframework.data.mongodb.test.util.IsBsonObject.*;
|
||||
import static org.springframework.data.mongodb.util.DBObjectUtils.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -402,6 +403,90 @@ public class ProjectionOperationUnitTests {
|
||||
is((Object) new BasicDBObject("$slice", Arrays.<Object> asList("$field", 5, 10))));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-784
|
||||
*/
|
||||
@Test
|
||||
public void shouldRenderCmpCorrectly() {
|
||||
|
||||
ProjectionOperation operation = Aggregation.project().and("field").cmp(10).as("cmp10");
|
||||
|
||||
assertThat(operation.toDBObject(Aggregation.DEFAULT_CONTEXT),
|
||||
isBsonObject().containing("$project.cmp10.$cmp.[0]", "$field").containing("$project.cmp10.$cmp.[1]", 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-784
|
||||
*/
|
||||
@Test
|
||||
public void shouldRenderEqCorrectly() {
|
||||
|
||||
ProjectionOperation operation = Aggregation.project().and("field").eq(10).as("eq10");
|
||||
|
||||
assertThat(operation.toDBObject(Aggregation.DEFAULT_CONTEXT),
|
||||
isBsonObject().containing("$project.eq10.$eq.[0]", "$field").containing("$project.eq10.$eq.[1]", 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-784
|
||||
*/
|
||||
@Test
|
||||
public void shouldRenderGtCorrectly() {
|
||||
|
||||
ProjectionOperation operation = Aggregation.project().and("field").gt(10).as("gt10");
|
||||
|
||||
assertThat(operation.toDBObject(Aggregation.DEFAULT_CONTEXT),
|
||||
isBsonObject().containing("$project.gt10.$gt.[0]", "$field").containing("$project.gt10.$gt.[1]", 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-784
|
||||
*/
|
||||
@Test
|
||||
public void shouldRenderGteCorrectly() {
|
||||
|
||||
ProjectionOperation operation = Aggregation.project().and("field").gte(10).as("gte10");
|
||||
|
||||
assertThat(operation.toDBObject(Aggregation.DEFAULT_CONTEXT),
|
||||
isBsonObject().containing("$project.gte10.$gte.[0]", "$field").containing("$project.gte10.$gte.[1]", 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-784
|
||||
*/
|
||||
@Test
|
||||
public void shouldRenderLtCorrectly() {
|
||||
|
||||
ProjectionOperation operation = Aggregation.project().and("field").lt(10).as("lt10");
|
||||
|
||||
assertThat(operation.toDBObject(Aggregation.DEFAULT_CONTEXT),
|
||||
isBsonObject().containing("$project.lt10.$lt.[0]", "$field").containing("$project.lt10.$lt.[1]", 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-784
|
||||
*/
|
||||
@Test
|
||||
public void shouldRenderLteCorrectly() {
|
||||
|
||||
ProjectionOperation operation = Aggregation.project().and("field").lte(10).as("lte10");
|
||||
|
||||
assertThat(operation.toDBObject(Aggregation.DEFAULT_CONTEXT),
|
||||
isBsonObject().containing("$project.lte10.$lte.[0]", "$field").containing("$project.lte10.$lte.[1]", 10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAMONGO-784
|
||||
*/
|
||||
@Test
|
||||
public void shouldRenderNeCorrectly() {
|
||||
|
||||
ProjectionOperation operation = Aggregation.project().and("field").ne(10).as("ne10");
|
||||
|
||||
assertThat(operation.toDBObject(Aggregation.DEFAULT_CONTEXT),
|
||||
isBsonObject().containing("$project.ne10.$ne.[0]", "$field").containing("$project.ne10.$ne.[1]", 10));
|
||||
}
|
||||
|
||||
private static DBObject exctractOperation(String field, DBObject fromProjectClause) {
|
||||
return (DBObject) fromProjectClause.get(field);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user