30
30
import org .apache .commons .logging .LogFactory ;
31
31
import org .bson .Document ;
32
32
import org .bson .conversions .Bson ;
33
-
34
33
import org .springframework .beans .BeansException ;
35
34
import org .springframework .context .ApplicationContext ;
36
35
import org .springframework .context .ApplicationContextAware ;
@@ -634,15 +633,17 @@ public MongoCollection<Document> createCollection(String collectionName,
634
633
}
635
634
636
635
@ Override
637
- public MongoCollection <Document > createView (String name , Class <?> source , AggregationPipeline pipeline , @ Nullable ViewOptions options ) {
636
+ public MongoCollection <Document > createView (String name , Class <?> source , AggregationPipeline pipeline ,
637
+ @ Nullable ViewOptions options ) {
638
638
639
639
return createView (name , getCollectionName (source ),
640
640
queryOperations .createAggregation (Aggregation .newAggregation (source , pipeline .getOperations ()), source ),
641
641
options );
642
642
}
643
643
644
644
@ Override
645
- public MongoCollection <Document > createView (String name , String source , AggregationPipeline pipeline , @ Nullable ViewOptions options ) {
645
+ public MongoCollection <Document > createView (String name , String source , AggregationPipeline pipeline ,
646
+ @ Nullable ViewOptions options ) {
646
647
647
648
return createView (name , source ,
648
649
queryOperations .createAggregation (Aggregation .newAggregation (pipeline .getOperations ()), (Class <?>) null ),
@@ -654,7 +655,8 @@ private MongoCollection<Document> createView(String name, String source, Aggrega
654
655
return doCreateView (name , source , aggregation .getAggregationPipeline (), options );
655
656
}
656
657
657
- protected MongoCollection <Document > doCreateView (String name , String source , List <Document > pipeline , @ Nullable ViewOptions options ) {
658
+ protected MongoCollection <Document > doCreateView (String name , String source , List <Document > pipeline ,
659
+ @ Nullable ViewOptions options ) {
658
660
659
661
CreateViewOptions viewOptions = new CreateViewOptions ();
660
662
if (options != null ) {
@@ -2065,7 +2067,16 @@ protected <O> AggregationResults<O> doAggregate(Aggregation aggregation, String
2065
2067
}
2066
2068
2067
2069
options .getComment ().ifPresent (aggregateIterable ::comment );
2068
- options .getHint ().ifPresent (aggregateIterable ::hint );
2070
+ if (options .getHintObject ().isPresent ()) {
2071
+ Object hintObject = options .getHintObject ().get ();
2072
+ if (hintObject instanceof String hintString ) {
2073
+ aggregateIterable = aggregateIterable .hintString (hintString );
2074
+ } else if (hintObject instanceof Document hintDocument ) {
2075
+ aggregateIterable = aggregateIterable .hint (hintDocument );
2076
+ } else {
2077
+ throw new IllegalStateException ("Unable to read hint of type %s" .formatted (hintObject .getClass ()));
2078
+ }
2079
+ }
2069
2080
2070
2081
if (options .hasExecutionTimeLimit ()) {
2071
2082
aggregateIterable = aggregateIterable .maxTime (options .getMaxTime ().toMillis (), TimeUnit .MILLISECONDS );
@@ -2124,7 +2135,16 @@ protected <O> Stream<O> aggregateStream(Aggregation aggregation, String collecti
2124
2135
}
2125
2136
2126
2137
options .getComment ().ifPresent (cursor ::comment );
2127
- options .getHint ().ifPresent (cursor ::hint );
2138
+ if (options .getHintObject ().isPresent ()) {
2139
+ Object hintObject = options .getHintObject ().get ();
2140
+ if (hintObject instanceof String hintString ) {
2141
+ cursor = cursor .hintString (hintString );
2142
+ } else if (hintObject instanceof Document hintDocument ) {
2143
+ cursor = cursor .hint (hintDocument );
2144
+ } else {
2145
+ throw new IllegalStateException ("Unable to read hint of type %s" .formatted (hintObject .getClass ()));
2146
+ }
2147
+ }
2128
2148
2129
2149
Class <?> domainType = aggregation instanceof TypedAggregation ? ((TypedAggregation ) aggregation ).getInputType ()
2130
2150
: null ;
0 commit comments