Skip to content

Commit b1cd7cf

Browse files
christophstroblmp911de
authored andcommitted
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.
1 parent 2ffac0a commit b1cd7cf

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationFunctionExpressions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
public enum AggregationFunctionExpressions {
3535

36-
SIZE, GTE;
36+
SIZE, CMP, EQ, GT, GTE, LT, LTE, NE;
3737

3838
/**
3939
* Returns an {@link AggregationExpression} build from the current {@link Enum} name and the given parameters.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,78 @@ public ProjectionOperationBuilder size() {
614614
return project("size");
615615
}
616616

617+
/**
618+
* Generates a {@code $cmp} expression (compare to) that compares the value of the field to a given value or field.
619+
*
620+
* @return never {@literal null}.
621+
* @since 1.10
622+
*/
623+
public ProjectionOperationBuilder cmp(Object compareValue) {
624+
return project("cmp", compareValue);
625+
}
626+
627+
/**
628+
* Generates a {@code $eq} expression (equal) that compares the value of the field to a given value or field.
629+
*
630+
* @return never {@literal null}.
631+
* @since 1.10
632+
*/
633+
public ProjectionOperationBuilder eq(Object compareValue) {
634+
return project("eq", compareValue);
635+
}
636+
637+
/**
638+
* Generates a {@code $gt} expression (greater than) that compares the value of the field to a given value or field.
639+
*
640+
* @return never {@literal null}.
641+
* @since 1.10
642+
*/
643+
public ProjectionOperationBuilder gt(Object compareValue) {
644+
return project("gt", compareValue);
645+
}
646+
647+
/**
648+
* Generates a {@code $gte} expression (greater than equal) that compares the value of the field to a given value or
649+
* field.
650+
*
651+
* @return never {@literal null}.
652+
* @since 1.10
653+
*/
654+
public ProjectionOperationBuilder gte(Object compareValue) {
655+
return project("gte", compareValue);
656+
}
657+
658+
/**
659+
* Generates a {@code $lt} expression (less than) that compares the value of the field to a given value or field.
660+
*
661+
* @return never {@literal null}.
662+
* @since 1.10
663+
*/
664+
public ProjectionOperationBuilder lt(Object compareValue) {
665+
return project("lt", compareValue);
666+
}
667+
668+
/**
669+
* Generates a {@code $lte} expression (less than equal) that compares the value of the field to a given value or
670+
* field.
671+
*
672+
* @return never {@literal null}.
673+
* @since 1.10
674+
*/
675+
public ProjectionOperationBuilder lte(Object compareValue) {
676+
return project("lte", compareValue);
677+
}
678+
679+
/**
680+
* Generates a {@code $ne} expression (not equal) that compares the value of the field to a given value or field.
681+
*
682+
* @return never {@literal null}.
683+
* @since 1.10
684+
*/
685+
public ProjectionOperationBuilder ne(Object compareValue) {
686+
return project("ne", compareValue);
687+
}
688+
617689
/**
618690
* Generates a {@code $slice} expression that returns a subset of the array held by the given field. <br />
619691
* If {@literal n} is positive, $slice returns up to the first n elements in the array. <br />

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.junit.Assert.*;
2020
import static org.springframework.data.mongodb.core.aggregation.AggregationFunctionExpressions.*;
2121
import static org.springframework.data.mongodb.core.aggregation.Fields.*;
22+
import static org.springframework.data.mongodb.test.util.IsBsonObject.*;
23+
import static org.springframework.data.mongodb.util.DBObjectUtils.*;
2224

2325
import java.util.Arrays;
2426
import java.util.List;
@@ -396,6 +398,90 @@ public void shouldRenderSliceWithPositionCorrectly() throws Exception {
396398
is((Object) new Document("$slice", Arrays.<Object> asList("$field", 5, 10))));
397399
}
398400

401+
/**
402+
* @see DATAMONGO-784
403+
*/
404+
@Test
405+
public void shouldRenderCmpCorrectly() {
406+
407+
ProjectionOperation operation = Aggregation.project().and("field").cmp(10).as("cmp10");
408+
409+
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT),
410+
isBsonObject().containing("$project.cmp10.$cmp.[0]", "$field").containing("$project.cmp10.$cmp.[1]", 10));
411+
}
412+
413+
/**
414+
* @see DATAMONGO-784
415+
*/
416+
@Test
417+
public void shouldRenderEqCorrectly() {
418+
419+
ProjectionOperation operation = Aggregation.project().and("field").eq(10).as("eq10");
420+
421+
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT),
422+
isBsonObject().containing("$project.eq10.$eq.[0]", "$field").containing("$project.eq10.$eq.[1]", 10));
423+
}
424+
425+
/**
426+
* @see DATAMONGO-784
427+
*/
428+
@Test
429+
public void shouldRenderGtCorrectly() {
430+
431+
ProjectionOperation operation = Aggregation.project().and("field").gt(10).as("gt10");
432+
433+
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT),
434+
isBsonObject().containing("$project.gt10.$gt.[0]", "$field").containing("$project.gt10.$gt.[1]", 10));
435+
}
436+
437+
/**
438+
* @see DATAMONGO-784
439+
*/
440+
@Test
441+
public void shouldRenderGteCorrectly() {
442+
443+
ProjectionOperation operation = Aggregation.project().and("field").gte(10).as("gte10");
444+
445+
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT),
446+
isBsonObject().containing("$project.gte10.$gte.[0]", "$field").containing("$project.gte10.$gte.[1]", 10));
447+
}
448+
449+
/**
450+
* @see DATAMONGO-784
451+
*/
452+
@Test
453+
public void shouldRenderLtCorrectly() {
454+
455+
ProjectionOperation operation = Aggregation.project().and("field").lt(10).as("lt10");
456+
457+
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT),
458+
isBsonObject().containing("$project.lt10.$lt.[0]", "$field").containing("$project.lt10.$lt.[1]", 10));
459+
}
460+
461+
/**
462+
* @see DATAMONGO-784
463+
*/
464+
@Test
465+
public void shouldRenderLteCorrectly() {
466+
467+
ProjectionOperation operation = Aggregation.project().and("field").lte(10).as("lte10");
468+
469+
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT),
470+
isBsonObject().containing("$project.lte10.$lte.[0]", "$field").containing("$project.lte10.$lte.[1]", 10));
471+
}
472+
473+
/**
474+
* @see DATAMONGO-784
475+
*/
476+
@Test
477+
public void shouldRenderNeCorrectly() {
478+
479+
ProjectionOperation operation = Aggregation.project().and("field").ne(10).as("ne10");
480+
481+
assertThat(operation.toDocument(Aggregation.DEFAULT_CONTEXT),
482+
isBsonObject().containing("$project.ne10.$ne.[0]", "$field").containing("$project.ne10.$ne.[1]", 10));
483+
}
484+
399485
private static Document exctractOperation(String field, Document fromProjectClause) {
400486
return (Document) fromProjectClause.get(field);
401487
}

0 commit comments

Comments
 (0)