DATAMONGO-1327 - Added support for $stdDevSamp and $stdDevPop to aggregation $group stage.

Original Pull Request: #360
CLA: 171720160409030719 (Gustavo de Geus)
This commit is contained in:
gustavodegeus
2016-04-09 11:46:55 -04:00
committed by Christoph Strobl
parent 5bd0e21173
commit 36838ffe31
2 changed files with 54 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,6 +39,7 @@ import com.mongodb.DBObject;
* @author Sebastian Herold
* @author Thomas Darimont
* @author Oliver Gierke
* @author Gustavo de Geus
* @since 1.3
*/
public class GroupOperation implements FieldsExposingAggregationOperation {
@@ -307,6 +308,27 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
return newBuilder(GroupOps.MAX, null, expr);
}
/**
* Generates an {@link GroupOperationBuilder} for an {@code $stdDevSamp}-expression that for the given
* field-reference.
*
* @param reference
* @return
*/
public GroupOperationBuilder stdDevSamp(String reference) {
return newBuilder(GroupOps.STD_DEV_SAMP, reference, null);
}
/**
* Generates an {@link GroupOperationBuilder} for an {@code $stdDevPop}-expression that for the given field-reference.
*
* @param reference
* @return
*/
public GroupOperationBuilder stdDevPop(String reference) {
return newBuilder(GroupOps.STD_DEV_POP, reference, null);
}
private GroupOperationBuilder newBuilder(Keyword keyword, String reference, Object value) {
return new GroupOperationBuilder(this, new Operation(keyword, null, reference, value));
}
@@ -371,7 +393,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
private static enum GroupOps implements Keyword {
SUM, LAST, FIRST, PUSH, AVG, MIN, MAX, ADD_TO_SET, COUNT;
SUM, LAST, FIRST, PUSH, AVG, MIN, MAX, ADD_TO_SET, COUNT, STD_DEV_SAMP, STD_DEV_POP;
@Override
public String toString() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ import com.mongodb.DBObject;
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Gustavo de Geus
*/
public class GroupOperationUnitTests {
@@ -204,6 +205,34 @@ public class GroupOperationUnitTests {
assertThat(tagsCount.get("$first"), is((Object) new BasicDBObject("$size", Arrays.asList("$tags"))));
}
/**
* @see DATAMONGO-1327
*/
@Test
public void groupOperationStdDevSampWithValue() {
GroupOperation groupOperation = Aggregation.group("a", "b").stdDevSamp("field").as("fieldStdDevSamp");
DBObject groupClause = extractDbObjectFromGroupOperation(groupOperation);
DBObject push = DBObjectTestUtils.getAsDBObject(groupClause, "fieldStdDevSamp");
assertThat(push, is((DBObject) new BasicDBObject("$stdDevSamp", "$field")));
}
/**
* @see DATAMONGO-1327
*/
@Test
public void groupOperationStdDevPopWithValue() {
GroupOperation groupOperation = Aggregation.group("a", "b").stdDevPop("field").as("fieldStdDevPop");
DBObject groupClause = extractDbObjectFromGroupOperation(groupOperation);
DBObject push = DBObjectTestUtils.getAsDBObject(groupClause, "fieldStdDevPop");
assertThat(push, is((DBObject) new BasicDBObject("$stdDevPop", "$field")));
}
private DBObject extractDbObjectFromGroupOperation(GroupOperation groupOperation) {
DBObject dbObject = groupOperation.toDBObject(Aggregation.DEFAULT_CONTEXT);
DBObject groupClause = DBObjectTestUtils.getAsDBObject(dbObject, "$group");