Skip to content

Commit f858376

Browse files
DATAMONGO-1536 - Add group operators that can be applied to $project stage (aggregation).
1 parent 09747e2 commit f858376

File tree

2 files changed

+444
-0
lines changed

2 files changed

+444
-0
lines changed

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

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,60 @@ private Subtract createSubtract() {
549549
public Trunc trunc() {
550550
return fieldRef != null ? Trunc.truncValueOf(fieldRef) : Trunc.truncValueOf(expression);
551551
}
552+
553+
/**
554+
* Calculates and returns the sum of numeric values.
555+
*
556+
* @return
557+
*/
558+
public Sum sum() {
559+
return fieldRef != null ? Sum.sumOf(fieldRef) : Sum.sumOf(expression);
560+
}
561+
562+
/**
563+
* Returns the average value of the numeric values.
564+
*
565+
* @return
566+
*/
567+
public Avg avg() {
568+
return fieldRef != null ? Avg.avgOf(fieldRef) : Avg.avgOf(expression);
569+
}
570+
571+
/**
572+
* Returns the maximum value.
573+
*
574+
* @return
575+
*/
576+
public Max max() {
577+
return fieldRef != null ? Max.maxOf(fieldRef) : Max.maxOf(expression);
578+
}
579+
580+
/**
581+
* Returns the minimum value.
582+
*
583+
* @return
584+
*/
585+
public Min min() {
586+
return fieldRef != null ? Min.minOf(fieldRef) : Min.minOf(expression);
587+
}
588+
589+
/**
590+
* Calculates the population standard deviation of the input values.
591+
*
592+
* @return
593+
*/
594+
public StdDevPop stdDevPop() {
595+
return fieldRef != null ? StdDevPop.stdDevPopOf(fieldRef) : StdDevPop.stdDevPopOf(expression);
596+
}
597+
598+
/**
599+
* Calculates the sample standard deviation of the input values.
600+
*
601+
* @return
602+
*/
603+
public StdDevSamp stdDevSamp() {
604+
return fieldRef != null ? StdDevSamp.stdDevSampOf(fieldRef) : StdDevSamp.stdDevSampOf(expression);
605+
}
552606
}
553607
}
554608

@@ -2771,4 +2825,274 @@ public interface FormatBuilder {
27712825
}
27722826
}
27732827

2828+
/**
2829+
* {@link AggregationExpression} for {@code $sum}.
2830+
*
2831+
* @author Christoph Strobl
2832+
*/
2833+
class Sum extends AbstractAggregationExpression {
2834+
2835+
private Sum(Object value) {
2836+
super(value);
2837+
}
2838+
2839+
@Override
2840+
public String getMongoMethod() {
2841+
return "$sum";
2842+
}
2843+
2844+
public static Sum sumOf(String fieldRef) {
2845+
return new Sum(asFields(fieldRef));
2846+
}
2847+
2848+
public static Sum sumOf(AggregationExpression expression) {
2849+
return new Sum(Collections.singletonList(expression));
2850+
}
2851+
2852+
public Sum and(String fieldRef) {
2853+
return new Sum(append(Fields.field(fieldRef)));
2854+
}
2855+
2856+
public Sum and(AggregationExpression expression) {
2857+
return new Sum(append(expression));
2858+
}
2859+
2860+
@Override
2861+
public DBObject toDbObject(Object value, AggregationOperationContext context) {
2862+
2863+
if (value instanceof List) {
2864+
if (((List) value).size() == 1) {
2865+
return super.toDbObject(((List<Object>) value).iterator().next(), context);
2866+
}
2867+
}
2868+
2869+
return super.toDbObject(value, context);
2870+
}
2871+
}
2872+
2873+
/**
2874+
* {@link AggregationExpression} for {@code $avg}.
2875+
*
2876+
* @author Christoph Strobl
2877+
*/
2878+
class Avg extends AbstractAggregationExpression {
2879+
2880+
private Avg(Object value) {
2881+
super(value);
2882+
}
2883+
2884+
@Override
2885+
public String getMongoMethod() {
2886+
return "$avg";
2887+
}
2888+
2889+
public static Avg avgOf(String fieldRef) {
2890+
return new Avg(asFields(fieldRef));
2891+
}
2892+
2893+
public static Avg avgOf(AggregationExpression expression) {
2894+
return new Avg(Collections.singletonList(expression));
2895+
}
2896+
2897+
public Avg and(String fieldRef) {
2898+
return new Avg(append(Fields.field(fieldRef)));
2899+
}
2900+
2901+
public Avg and(AggregationExpression expression) {
2902+
return new Avg(append(expression));
2903+
}
2904+
2905+
@Override
2906+
public DBObject toDbObject(Object value, AggregationOperationContext context) {
2907+
2908+
if (value instanceof List) {
2909+
if (((List) value).size() == 1) {
2910+
return super.toDbObject(((List<Object>) value).iterator().next(), context);
2911+
}
2912+
}
2913+
2914+
return super.toDbObject(value, context);
2915+
}
2916+
}
2917+
2918+
/**
2919+
* {@link AggregationExpression} for {@code $max}.
2920+
*
2921+
* @author Christoph Strobl
2922+
*/
2923+
class Max extends AbstractAggregationExpression {
2924+
2925+
private Max(Object value) {
2926+
super(value);
2927+
}
2928+
2929+
@Override
2930+
public String getMongoMethod() {
2931+
return "$max";
2932+
}
2933+
2934+
public static Max maxOf(String fieldRef) {
2935+
return new Max(asFields(fieldRef));
2936+
}
2937+
2938+
public static Max maxOf(AggregationExpression expression) {
2939+
return new Max(Collections.singletonList(expression));
2940+
}
2941+
2942+
public Max and(String fieldRef) {
2943+
return new Max(append(Fields.field(fieldRef)));
2944+
}
2945+
2946+
public Max and(AggregationExpression expression) {
2947+
return new Max(append(expression));
2948+
}
2949+
2950+
@Override
2951+
public DBObject toDbObject(Object value, AggregationOperationContext context) {
2952+
2953+
if (value instanceof List) {
2954+
if (((List) value).size() == 1) {
2955+
return super.toDbObject(((List<Object>) value).iterator().next(), context);
2956+
}
2957+
}
2958+
2959+
return super.toDbObject(value, context);
2960+
}
2961+
}
2962+
2963+
/**
2964+
* {@link AggregationExpression} for {@code $min}.
2965+
*
2966+
* @author Christoph Strobl
2967+
*/
2968+
class Min extends AbstractAggregationExpression {
2969+
2970+
private Min(Object value) {
2971+
super(value);
2972+
}
2973+
2974+
@Override
2975+
public String getMongoMethod() {
2976+
return "$min";
2977+
}
2978+
2979+
public static Min minOf(String fieldRef) {
2980+
return new Min(asFields(fieldRef));
2981+
}
2982+
2983+
public static Min minOf(AggregationExpression expression) {
2984+
return new Min(Collections.singletonList(expression));
2985+
}
2986+
2987+
public Min and(String fieldRef) {
2988+
return new Min(append(Fields.field(fieldRef)));
2989+
}
2990+
2991+
public Min and(AggregationExpression expression) {
2992+
return new Min(append(expression));
2993+
}
2994+
2995+
@Override
2996+
public DBObject toDbObject(Object value, AggregationOperationContext context) {
2997+
2998+
if (value instanceof List) {
2999+
if (((List) value).size() == 1) {
3000+
return super.toDbObject(((List<Object>) value).iterator().next(), context);
3001+
}
3002+
}
3003+
3004+
return super.toDbObject(value, context);
3005+
}
3006+
}
3007+
3008+
/**
3009+
* {@link AggregationExpression} for {@code $stdDevPop}.
3010+
*
3011+
* @author Christoph Strobl
3012+
*/
3013+
class StdDevPop extends AbstractAggregationExpression {
3014+
3015+
private StdDevPop(Object value) {
3016+
super(value);
3017+
}
3018+
3019+
@Override
3020+
public String getMongoMethod() {
3021+
return "$stdDevPop";
3022+
}
3023+
3024+
public static StdDevPop stdDevPopOf(String fieldRef) {
3025+
return new StdDevPop(asFields(fieldRef));
3026+
}
3027+
3028+
public static StdDevPop stdDevPopOf(AggregationExpression expression) {
3029+
return new StdDevPop(Collections.singletonList(expression));
3030+
}
3031+
3032+
public StdDevPop and(String fieldRef) {
3033+
return new StdDevPop(append(Fields.field(fieldRef)));
3034+
}
3035+
3036+
public StdDevPop and(AggregationExpression expression) {
3037+
return new StdDevPop(append(expression));
3038+
}
3039+
3040+
@Override
3041+
public DBObject toDbObject(Object value, AggregationOperationContext context) {
3042+
3043+
if (value instanceof List) {
3044+
if (((List) value).size() == 1) {
3045+
return super.toDbObject(((List<Object>) value).iterator().next(), context);
3046+
}
3047+
}
3048+
3049+
return super.toDbObject(value, context);
3050+
}
3051+
}
3052+
3053+
/**
3054+
* {@link AggregationExpression} for {@code $stdDevSamp}.
3055+
*
3056+
* @author Christoph Strobl
3057+
*/
3058+
class StdDevSamp extends AbstractAggregationExpression {
3059+
3060+
private StdDevSamp(Object value) {
3061+
super(value);
3062+
}
3063+
3064+
@Override
3065+
public String getMongoMethod() {
3066+
return "$stdDevSamp";
3067+
}
3068+
3069+
public static StdDevSamp stdDevSampOf(String fieldRef) {
3070+
return new StdDevSamp(asFields(fieldRef));
3071+
}
3072+
3073+
public static StdDevSamp stdDevSampOf(AggregationExpression expression) {
3074+
return new StdDevSamp(Collections.singletonList(expression));
3075+
}
3076+
3077+
public StdDevSamp and(String fieldRef) {
3078+
return new StdDevSamp(append(Fields.field(fieldRef)));
3079+
}
3080+
3081+
public StdDevSamp and(AggregationExpression expression) {
3082+
return new StdDevSamp(append(expression));
3083+
}
3084+
3085+
@Override
3086+
public DBObject toDbObject(Object value, AggregationOperationContext context) {
3087+
3088+
if (value instanceof List) {
3089+
if (((List) value).size() == 1) {
3090+
return super.toDbObject(((List<Object>) value).iterator().next(), context);
3091+
}
3092+
}
3093+
3094+
return super.toDbObject(value, context);
3095+
}
3096+
}
3097+
27743098
}

0 commit comments

Comments
 (0)