@@ -2116,7 +2116,8 @@ protected <O> AggregationResults<O> aggregate(Aggregation aggregation, String co
2116
2116
Assert .notNull (aggregation , "Aggregation pipeline must not be null!" );
2117
2117
Assert .notNull (outputType , "Output type must not be null!" );
2118
2118
2119
- return doAggregate (aggregation , collectionName , outputType , prepareAggregationContext (aggregation , context ));
2119
+ AggregationOperationContext contextToUse = new AggregationUtil (queryMapper , mappingContext ).prepareAggregationContext (aggregation , context );
2120
+ return doAggregate (aggregation , collectionName , outputType , contextToUse );
2120
2121
}
2121
2122
2122
2123
@ SuppressWarnings ("ConstantConditions" )
@@ -2126,10 +2127,11 @@ protected <O> AggregationResults<O> doAggregate(Aggregation aggregation, String
2126
2127
DocumentCallback <O > callback = new UnwrapAndReadDocumentCallback <>(mongoConverter , outputType , collectionName );
2127
2128
2128
2129
AggregationOptions options = aggregation .getOptions ();
2130
+ AggregationUtil aggregationUtil = new AggregationUtil (queryMapper , mappingContext );
2129
2131
2130
2132
if (options .isExplain ()) {
2131
2133
2132
- Document command = aggregationToCommand (collectionName , aggregation , context );
2134
+ Document command = aggregationUtil . createCommand (collectionName , aggregation , context );
2133
2135
2134
2136
if (LOGGER .isDebugEnabled ()) {
2135
2137
LOGGER .debug ("Executing aggregation: {}" , serializeToJsonSafely (command ));
@@ -2140,7 +2142,7 @@ protected <O> AggregationResults<O> doAggregate(Aggregation aggregation, String
2140
2142
.map (callback ::doWith ).collect (Collectors .toList ()), commandResult );
2141
2143
}
2142
2144
2143
- List <Document > pipeline = aggregationToPipeline (aggregation , context );
2145
+ List <Document > pipeline = aggregationUtil . createPipeline (aggregation , context );
2144
2146
2145
2147
if (LOGGER .isDebugEnabled ()) {
2146
2148
LOGGER .debug ("Executing aggregation: {} in collection {}" , serializeToJsonSafely (pipeline ), collectionName );
@@ -2178,10 +2180,11 @@ protected <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, Stri
2178
2180
Assert .notNull (outputType , "Output type must not be null!" );
2179
2181
Assert .isTrue (!aggregation .getOptions ().isExplain (), "Can't use explain option with streaming!" );
2180
2182
2181
- AggregationOperationContext rootContext = prepareAggregationContext (aggregation , context );
2183
+ AggregationUtil aggregationUtil = new AggregationUtil (queryMapper , mappingContext );
2184
+ AggregationOperationContext rootContext = aggregationUtil .prepareAggregationContext (aggregation , context );
2182
2185
2183
2186
AggregationOptions options = aggregation .getOptions ();
2184
- List <Document > pipeline = aggregationToPipeline (aggregation , rootContext );
2187
+ List <Document > pipeline = aggregationUtil . createPipeline (aggregation , rootContext );
2185
2188
2186
2189
if (LOGGER .isDebugEnabled ()) {
2187
2190
LOGGER .debug ("Streaming aggregation: {} in collection {}" , serializeToJsonSafely (pipeline ), collectionName );
@@ -2844,72 +2847,6 @@ private Document addFieldsForProjection(Document fields, Class<?> domainType, Cl
2844
2847
return fields ;
2845
2848
}
2846
2849
2847
- /**
2848
- * Prepare the {@link AggregationOperationContext} for a given aggregation by either returning the context itself it
2849
- * is not {@literal null}, create a {@link TypeBasedAggregationOperationContext} if the aggregation contains type
2850
- * information (is a {@link TypedAggregation}) or use the {@link Aggregation#DEFAULT_CONTEXT}.
2851
- *
2852
- * @param aggregation must not be {@literal null}.
2853
- * @param context can be {@literal null}.
2854
- * @return the root {@link AggregationOperationContext} to use.
2855
- */
2856
- private AggregationOperationContext prepareAggregationContext (Aggregation aggregation ,
2857
- @ Nullable AggregationOperationContext context ) {
2858
-
2859
- if (context != null ) {
2860
- return context ;
2861
- }
2862
-
2863
- if (aggregation instanceof TypedAggregation ) {
2864
- return new TypeBasedAggregationOperationContext (((TypedAggregation ) aggregation ).getInputType (), mappingContext ,
2865
- queryMapper );
2866
- }
2867
-
2868
- return Aggregation .DEFAULT_CONTEXT ;
2869
- }
2870
-
2871
- /**
2872
- * Extract and map the aggregation pipeline.
2873
- *
2874
- * @param aggregation
2875
- * @param context
2876
- * @return
2877
- */
2878
- private List <Document > aggregationToPipeline (Aggregation aggregation , AggregationOperationContext context ) {
2879
-
2880
- if (!ObjectUtils .nullSafeEquals (context , Aggregation .DEFAULT_CONTEXT )) {
2881
- return aggregation .toPipeline (context );
2882
- }
2883
-
2884
- return mapAggregationPipeline (aggregation .toPipeline (context ));
2885
- }
2886
-
2887
- /**
2888
- * Extract the command and map the aggregation pipeline.
2889
- *
2890
- * @param aggregation
2891
- * @param context
2892
- * @return
2893
- */
2894
- private Document aggregationToCommand (String collection , Aggregation aggregation ,
2895
- AggregationOperationContext context ) {
2896
-
2897
- Document command = aggregation .toDocument (collection , context );
2898
-
2899
- if (!ObjectUtils .nullSafeEquals (context , Aggregation .DEFAULT_CONTEXT )) {
2900
- return command ;
2901
- }
2902
-
2903
- command .put ("pipeline" , mapAggregationPipeline (command .get ("pipeline" , List .class )));
2904
-
2905
- return command ;
2906
- }
2907
-
2908
- private List <Document > mapAggregationPipeline (List <Document > pipeline ) {
2909
-
2910
- return pipeline .stream ().map (val -> queryMapper .getMappedObject (val , Optional .empty ()))
2911
- .collect (Collectors .toList ());
2912
- }
2913
2850
2914
2851
/**
2915
2852
* Tries to convert the given {@link RuntimeException} into a {@link DataAccessException} but returns the original
0 commit comments