28
28
import org .springframework .data .mongodb .core .aggregation .ExposedFields .DirectFieldReference ;
29
29
import org .springframework .data .mongodb .core .aggregation .ExposedFields .ExposedField ;
30
30
import org .springframework .data .mongodb .core .aggregation .ExposedFields .FieldReference ;
31
+ import org .springframework .data .mongodb .core .aggregation .FacetOperation .FacetOperationBuilder ;
31
32
import org .springframework .data .mongodb .core .aggregation .FieldsExposingAggregationOperation .InheritsFieldsAggregationOperation ;
32
33
import org .springframework .data .mongodb .core .aggregation .GraphLookupOperation .StartWithBuilder ;
33
34
import org .springframework .data .mongodb .core .aggregation .ReplaceRootOperation .ReplaceRootDocumentOperationBuilder ;
@@ -66,7 +67,7 @@ public class Aggregation {
66
67
*/
67
68
public static final String CURRENT = SystemVariable .CURRENT .toString ();
68
69
69
- public static final AggregationOperationContext DEFAULT_CONTEXT = new NoOpAggregationOperationContext () ;
70
+ public static final AggregationOperationContext DEFAULT_CONTEXT = AggregationOperationRenderer . DEFAULT_CONTEXT ;
70
71
public static final AggregationOptions DEFAULT_OPTIONS = newAggregationOptions ().build ();
71
72
72
73
protected final List <AggregationOperation > operations ;
@@ -458,6 +459,25 @@ public static BucketAutoOperation bucketAuto(AggregationExpression groupByExpres
458
459
return new BucketAutoOperation (groupByExpression , buckets );
459
460
}
460
461
462
+ /**
463
+ * Creates a new {@link FacetOperation}.
464
+ *
465
+ * @return
466
+ */
467
+ public static FacetOperation facet () {
468
+ return FacetOperation .EMPTY ;
469
+ }
470
+
471
+ /**
472
+ * Creates a new {@link FacetOperationBuilder} given {@link Aggregation}.
473
+ *
474
+ * @param aggregationOperations the sub-pipeline, must not be {@literal null}.
475
+ * @return
476
+ */
477
+ public static FacetOperationBuilder facet (AggregationOperation ... aggregationOperations ) {
478
+ return facet ().and (aggregationOperations );
479
+ }
480
+
461
481
/**
462
482
* Creates a new {@link LookupOperation}.
463
483
*
@@ -549,26 +569,7 @@ public static AggregationOptions.Builder newAggregationOptions() {
549
569
*/
550
570
public Document toDocument (String inputCollectionName , AggregationOperationContext rootContext ) {
551
571
552
- AggregationOperationContext context = rootContext ;
553
- List <Document > operationDocuments = new ArrayList <Document >(operations .size ());
554
-
555
- for (AggregationOperation operation : operations ) {
556
-
557
- operationDocuments .add (operation .toDocument (context ));
558
-
559
- if (operation instanceof FieldsExposingAggregationOperation ) {
560
-
561
- FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation ) operation ;
562
- ExposedFields fields = exposedFieldsOperation .getFields ();
563
-
564
- if (operation instanceof InheritsFieldsAggregationOperation ) {
565
- context = new InheritingExposedFieldsAggregationOperationContext (fields , context );
566
- } else {
567
- context = fields .exposesNoFields () ? DEFAULT_CONTEXT
568
- : new ExposedFieldsAggregationOperationContext (fields , context );
569
- }
570
- }
571
- }
572
+ List <Document > operationDocuments = AggregationOperationRenderer .toDocument (operations , rootContext );
572
573
573
574
Document command = new Document ("aggregate" , inputCollectionName );
574
575
command .put ("pipeline" , operationDocuments );
@@ -584,43 +585,7 @@ public Document toDocument(String inputCollectionName, AggregationOperationConte
584
585
*/
585
586
@ Override
586
587
public String toString () {
587
- return SerializationUtils
588
- .serializeToJsonSafely (toDocument ("__collection__" , new NoOpAggregationOperationContext ()));
589
- }
590
-
591
- /**
592
- * Simple {@link AggregationOperationContext} that just returns {@link FieldReference}s as is.
593
- *
594
- * @author Oliver Gierke
595
- */
596
- private static class NoOpAggregationOperationContext implements AggregationOperationContext {
597
-
598
- /*
599
- * (non-Javadoc)
600
- * @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getMappedObject(com.mongodb.Document)
601
- */
602
- @ Override
603
- public Document getMappedObject (Document document ) {
604
- return document ;
605
- }
606
-
607
- /*
608
- * (non-Javadoc)
609
- * @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getReference(org.springframework.data.mongodb.core.aggregation.ExposedFields.AvailableField)
610
- */
611
- @ Override
612
- public FieldReference getReference (Field field ) {
613
- return new DirectFieldReference (new ExposedField (field , true ));
614
- }
615
-
616
- /*
617
- * (non-Javadoc)
618
- * @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getReference(java.lang.String)
619
- */
620
- @ Override
621
- public FieldReference getReference (String name ) {
622
- return new DirectFieldReference (new ExposedField (new AggregationField (name ), true ));
623
- }
588
+ return SerializationUtils .serializeToJsonSafely (toDocument ("__collection__" , DEFAULT_CONTEXT ));
624
589
}
625
590
626
591
/**
@@ -660,7 +625,7 @@ public static boolean isReferingToSystemVariable(String fieldRef) {
660
625
return false ;
661
626
}
662
627
663
- /*
628
+ /*
664
629
* (non-Javadoc)
665
630
* @see java.lang.Enum#toString()
666
631
*/
0 commit comments