diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java index 4ee8b37ed..a3d714970 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java @@ -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() { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GroupOperationUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GroupOperationUnitTests.java index e2bcb939d..0bb274e85 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GroupOperationUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/GroupOperationUnitTests.java @@ -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");