Skip to content

Commit 19e6278

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2651 - Support $accumulator in GroupOperationBuilder.
Original pull request: #898.
1 parent b8298ed commit 19e6278

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

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

+24-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.bson.Document;
2424
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
2525
import org.springframework.data.mongodb.core.aggregation.ExposedFields.FieldReference;
26+
import org.springframework.data.mongodb.core.aggregation.ScriptOperators.Accumulator;
2627
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
2829

@@ -375,6 +376,17 @@ public GroupOperationBuilder stdDevPop(AggregationExpression expr) {
375376
return newBuilder(GroupOps.STD_DEV_POP, null, expr);
376377
}
377378

379+
/**
380+
* Generates an {@link GroupOperationBuilder} for an {@code $accumulator}-expression.
381+
*
382+
* @param accumulator must not be {@literal null}.
383+
* @return never {@literal null}.
384+
* @since 1.10
385+
*/
386+
public GroupOperationBuilder accumulate(Accumulator accumulator) {
387+
return new GroupOperationBuilder(this, new Operation(accumulator));
388+
}
389+
378390
private GroupOperationBuilder newBuilder(Keyword keyword, @Nullable String reference, @Nullable Object value) {
379391
return new GroupOperationBuilder(this, new Operation(keyword, null, reference, value));
380392
}
@@ -465,12 +477,16 @@ public String toString() {
465477

466478
static class Operation implements AggregationOperation {
467479

468-
private final Keyword op;
480+
private final @Nullable Keyword op;
469481
private final @Nullable String key;
470482
private final @Nullable String reference;
471483
private final @Nullable Object value;
472484

473-
public Operation(Keyword op, @Nullable String key, @Nullable String reference, @Nullable Object value) {
485+
Operation(AggregationExpression expression) {
486+
this(null, null, null, expression);
487+
}
488+
489+
public Operation(@Nullable Keyword op, @Nullable String key, @Nullable String reference, @Nullable Object value) {
474490

475491
this.op = op;
476492
this.key = key;
@@ -487,7 +503,12 @@ public ExposedField asField() {
487503
}
488504

489505
public Document toDocument(AggregationOperationContext context) {
490-
return new Document(key, new Document(op.toString(), getValue(context)));
506+
507+
Object value = getValue(context);
508+
if(op == null && value instanceof Document) {
509+
return new Document(key, value);
510+
}
511+
return new Document(key, new Document(op.toString(), value));
491512
}
492513

493514
public Object getValue(AggregationOperationContext context) {

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.bson.Document;
2525
import org.junit.jupiter.api.Test;
26-
2726
import org.springframework.data.mongodb.core.DocumentTestUtils;
2827
import org.springframework.data.mongodb.core.query.Criteria;
2928

@@ -241,6 +240,20 @@ public void sumWithNullExpressionShouldThrowException() {
241240
.isThrownBy(() -> Aggregation.group("username").sum((AggregationExpression) null));
242241
}
243242

243+
@Test // DATAMONGO-2651
244+
void accumulatorShouldBeAllowedOnGroupOperation() {
245+
246+
GroupOperation groupOperation = Aggregation.group("id")
247+
.accumulate(
248+
ScriptOperators.accumulatorBuilder().init("inti").accumulate("acc").merge("merge").finalize("finalize"))
249+
.as("accumulated-value");
250+
251+
Document groupClause = extractDocumentFromGroupOperation(groupOperation);
252+
Document accumulatedValue = DocumentTestUtils.getAsDocument(groupClause, "accumulated-value");
253+
254+
assertThat(accumulatedValue.get("$accumulator")).isNotNull();
255+
}
256+
244257
private Document extractDocumentFromGroupOperation(GroupOperation groupOperation) {
245258
Document document = groupOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
246259
Document groupClause = DocumentTestUtils.getAsDocument(document, "$group");

0 commit comments

Comments
 (0)