Skip to content

Commit 36838ff

Browse files
gustavodegeuschristophstrobl
authored andcommitted
DATAMONGO-1327 - Added support for $stdDevSamp and $stdDevPop to aggregation $group stage.
Original Pull Request: #360 CLA: 171720160409030719 (Gustavo de Geus)
1 parent 5bd0e21 commit 36838ff

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@
3939
* @author Sebastian Herold
4040
* @author Thomas Darimont
4141
* @author Oliver Gierke
42+
* @author Gustavo de Geus
4243
* @since 1.3
4344
*/
4445
public class GroupOperation implements FieldsExposingAggregationOperation {
@@ -307,6 +308,27 @@ public GroupOperationBuilder max(AggregationExpression expr) {
307308
return newBuilder(GroupOps.MAX, null, expr);
308309
}
309310

311+
/**
312+
* Generates an {@link GroupOperationBuilder} for an {@code $stdDevSamp}-expression that for the given
313+
* field-reference.
314+
*
315+
* @param reference
316+
* @return
317+
*/
318+
public GroupOperationBuilder stdDevSamp(String reference) {
319+
return newBuilder(GroupOps.STD_DEV_SAMP, reference, null);
320+
}
321+
322+
/**
323+
* Generates an {@link GroupOperationBuilder} for an {@code $stdDevPop}-expression that for the given field-reference.
324+
*
325+
* @param reference
326+
* @return
327+
*/
328+
public GroupOperationBuilder stdDevPop(String reference) {
329+
return newBuilder(GroupOps.STD_DEV_POP, reference, null);
330+
}
331+
310332
private GroupOperationBuilder newBuilder(Keyword keyword, String reference, Object value) {
311333
return new GroupOperationBuilder(this, new Operation(keyword, null, reference, value));
312334
}
@@ -371,7 +393,7 @@ interface Keyword {
371393

372394
private static enum GroupOps implements Keyword {
373395

374-
SUM, LAST, FIRST, PUSH, AVG, MIN, MAX, ADD_TO_SET, COUNT;
396+
SUM, LAST, FIRST, PUSH, AVG, MIN, MAX, ADD_TO_SET, COUNT, STD_DEV_SAMP, STD_DEV_POP;
375397

376398
@Override
377399
public String toString() {

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2015 the original author or authors.
2+
* Copyright 2013-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
*
3434
* @author Oliver Gierke
3535
* @author Thomas Darimont
36+
* @author Gustavo de Geus
3637
*/
3738
public class GroupOperationUnitTests {
3839

@@ -204,6 +205,34 @@ public void shouldRenderSizeExpressionInGroup() {
204205
assertThat(tagsCount.get("$first"), is((Object) new BasicDBObject("$size", Arrays.asList("$tags"))));
205206
}
206207

208+
/**
209+
* @see DATAMONGO-1327
210+
*/
211+
@Test
212+
public void groupOperationStdDevSampWithValue() {
213+
214+
GroupOperation groupOperation = Aggregation.group("a", "b").stdDevSamp("field").as("fieldStdDevSamp");
215+
216+
DBObject groupClause = extractDbObjectFromGroupOperation(groupOperation);
217+
DBObject push = DBObjectTestUtils.getAsDBObject(groupClause, "fieldStdDevSamp");
218+
219+
assertThat(push, is((DBObject) new BasicDBObject("$stdDevSamp", "$field")));
220+
}
221+
222+
/**
223+
* @see DATAMONGO-1327
224+
*/
225+
@Test
226+
public void groupOperationStdDevPopWithValue() {
227+
228+
GroupOperation groupOperation = Aggregation.group("a", "b").stdDevPop("field").as("fieldStdDevPop");
229+
230+
DBObject groupClause = extractDbObjectFromGroupOperation(groupOperation);
231+
DBObject push = DBObjectTestUtils.getAsDBObject(groupClause, "fieldStdDevPop");
232+
233+
assertThat(push, is((DBObject) new BasicDBObject("$stdDevPop", "$field")));
234+
}
235+
207236
private DBObject extractDbObjectFromGroupOperation(GroupOperation groupOperation) {
208237
DBObject dbObject = groupOperation.toDBObject(Aggregation.DEFAULT_CONTEXT);
209238
DBObject groupClause = DBObjectTestUtils.getAsDBObject(dbObject, "$group");

0 commit comments

Comments
 (0)