23
23
import org .bson .Document ;
24
24
import org .springframework .data .mongodb .core .aggregation .ExposedFields .ExposedField ;
25
25
import org .springframework .data .mongodb .core .aggregation .ExposedFields .FieldReference ;
26
+ import org .springframework .data .mongodb .core .aggregation .ScriptOperators .Accumulator ;
26
27
import org .springframework .lang .Nullable ;
27
28
import org .springframework .util .Assert ;
28
29
@@ -375,6 +376,17 @@ public GroupOperationBuilder stdDevPop(AggregationExpression expr) {
375
376
return newBuilder (GroupOps .STD_DEV_POP , null , expr );
376
377
}
377
378
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
+
378
390
private GroupOperationBuilder newBuilder (Keyword keyword , @ Nullable String reference , @ Nullable Object value ) {
379
391
return new GroupOperationBuilder (this , new Operation (keyword , null , reference , value ));
380
392
}
@@ -465,12 +477,16 @@ public String toString() {
465
477
466
478
static class Operation implements AggregationOperation {
467
479
468
- private final Keyword op ;
480
+ private final @ Nullable Keyword op ;
469
481
private final @ Nullable String key ;
470
482
private final @ Nullable String reference ;
471
483
private final @ Nullable Object value ;
472
484
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 ) {
474
490
475
491
this .op = op ;
476
492
this .key = key ;
@@ -487,7 +503,12 @@ public ExposedField asField() {
487
503
}
488
504
489
505
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 ));
491
512
}
492
513
493
514
public Object getValue (AggregationOperationContext context ) {
0 commit comments