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 2c3cbb3613
commit 5d2faef072
2 changed files with 55 additions and 1 deletions

View File

@@ -27,6 +27,9 @@ import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldRefe
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
/**
* Encapsulates the aggregation framework {@code $group}-operation.
* <p>
@@ -37,6 +40,7 @@ import org.springframework.util.StringUtils;
* @author Sebastian Herold
* @author Thomas Darimont
* @author Oliver Gierke
* @author Gustavo de Geus
* @author Christoph Strobl
* @since 1.3
*/
@@ -311,6 +315,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));
}
@@ -375,7 +400,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

@@ -31,6 +31,7 @@ import org.springframework.data.mongodb.core.DocumentTestUtils;
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Gustavo de Geus
*/
public class GroupOperationUnitTests {
@@ -202,6 +203,34 @@ public class GroupOperationUnitTests {
assertThat(tagsCount.get("$first"), is((Object) new Document("$size", Arrays.asList("$tags"))));
}
/**
* @see DATAMONGO-1327
*/
@Test
public void groupOperationStdDevSampWithValue() {
GroupOperation groupOperation = Aggregation.group("a", "b").stdDevSamp("field").as("fieldStdDevSamp");
Document groupClause = extractDocumentFromGroupOperation(groupOperation);
Document push = DocumentTestUtils.getAsDocument(groupClause, "fieldStdDevSamp");
assertThat(push, is(new Document("$stdDevSamp", "$field")));
}
/**
* @see DATAMONGO-1327
*/
@Test
public void groupOperationStdDevPopWithValue() {
GroupOperation groupOperation = Aggregation.group("a", "b").stdDevPop("field").as("fieldStdDevPop");
Document groupClause = extractDocumentFromGroupOperation(groupOperation);
Document push = DocumentTestUtils.getAsDocument(groupClause, "fieldStdDevPop");
assertThat(push, is(new Document("$stdDevPop", "$field")));
}
private Document extractDocumentFromGroupOperation(GroupOperation groupOperation) {
Document document = groupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
Document groupClause = DocumentTestUtils.getAsDocument(document, "$group");